In a UI Page, setting a $scope variable in a callback does not update the HTML

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 05:11 AM - edited 04-01-2025 05:19 AM
So I have an odd one.. I have inherited a UI Page, who builds multiple $scope variables using nested $HTTP calls.
I thought this was daft, as 90% of the data could have been gathered from one GlideRecord using .dot walking.
I created a script include and use GlideAjax to call it. This works and I can see the JSON/Object text that comes back is correct.
Then I ran into the issue that the $scope variable I used was not being set or at least passed into the HTML.
Doing some testing but just setting the scope variable manually show that it works outside the GA callback, but not inside. The code inside is being called as the Alert from the answer is working.
Manual setting:
$scope.task = {"number": "fakenumber"}
Code from UI Page's Client controller:
var wsGA = new GlideAjax('<script include>');
wsGA.addParam('sysparm_name', '<script include function>');
wsGA.addParam('sysparm_wstask', taskID);
wsGA.addParam('sysparm_userid', g_user.userID);
wsGA.getXML(processResult);
function processResult(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
//Setting the scope from JSON variable
$scope.task = answer.task;
//Manual setting
$scope.task = '{"number": "Test"}';
}
I cannot find anything online that shouldn't work, but it certainly does not work as I expected.
Paul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2025 05:32 AM
Hi @Paul Ryder1,
Use $scope.$apply(); after you manually set the variable. This will update the binding variables.
Thanks,
Prashanth