service portal how to get data from server to client

sam1212
Mega Expert

Hi All,

This looks simple but i am unable to figure out

i am getting the data from server to client but i unable to access the variable outside the function

here i am getting the watchlist from server and keeping in $scope.ta1 but if i try to keep outside the function i am not able to access

spUtil.update($scope).then(function(data){

              $scope.ta1=data.watchlistValues[0].name;

alert($scope.ta1);//working good

});

alert($scope.ta1);//not working

Any help is appreciated.

11 REPLIES 11

sam1212
Mega Expert

nathanfirthfschuster can you guys some suggestion on this


Try something like this instead:



 


spUtil.update($scope).then(function(response){


        data.ta1=response.watchlistValues[0].name;


        alert(data.ta1);


});


alert(data.ta1);


Thanks Nath for reply but this not working ..


issue: data not defiined and then even if i try to define it is giving undefined   result for alert outside the function.



I have a question if this is not working then how can we pass result to function which we call from HTML


like for example



in html i am calling a function



<tags-input ng-model="c.data.watchlistnew" display-property="name" placeholder="User Names / IDs" replace-spaces-with-dashes="false" add-from-autocomplete-only="true">


                                                                      <auto-complete source="loadUsers($query,'watchlist')"


                                                                                                min-length="3"


                                                                                                load-on-focus="false"


                                                                                                load-on-empty="false"


                                                                                                debounce-delay="300"


                                                                                                max-results-to-show="10"></auto-complete>



</tags-input>




client script:


$scope.loadUsers = function($query, type) {


             


     


              var params = {"type" : type,"q": $query};


                  var q = encodeURIComponent(params.q);


              $scope.data.watchlistValue=q;



     


spUtil.update($scope).then(function(response){    


        $scope.t1=response.watchlistValues[0].name;    


        alert($scope.t1);    


      });    


      alert($scope.t1);



return $scope.t1


      };



server




data.watchlistValues=[];


     



if(input.watchlistValue)


      {



      var user = new GlideRecord('sys_user');


      user.addQuery('user_name','CONTAINS','sam');


      user.addQuery('active','true');


      user.query();


      while(user.next()){


             


              var structuredWatchlist=   {};


      structuredWatchlist.name=user.getValue('first_name')+" "+user.getValue('last_name');


      structuredWatchlist.sys_id=       user.getValue('sys_id');


      structuredWatchlist.first_name=user.getValue('first_name');


      structuredWatchlist.last_name=user.getValue('last_name');


             


      data.watchlistValues.push(structuredWatchlist);


      }


     


}



SO here i am getting the data from server based on the input value.. and trying to get the value to controller and pass it back to HTML. Can you provide your suggestion on how to over come this kind of scenario.



Thanks an advance.


Try using c.data instead of just data. Also make sure your controler includes "var c = this" at the top.