service portal how to get data from server to client
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2017 04:25 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2017 04:26 PM
nathanfirthfschuster can you guys some suggestion on this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2017 04:38 PM
Try something like this instead:
spUtil.update($scope).then(function(response){
data.ta1=response.watchlistValues[0].name;
alert(data.ta1);
});
alert(data.ta1);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2017 08:24 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2017 08:32 PM
Try using c.data instead of just data. Also make sure your controler includes "var c = this" at the top.