Utilizing a socket, I am fetching data into an angularJS controller.
$rootScope.list1= '';
socket.emit('ticker', symbol);
socket.on('quote', function(data) {
$rootScope.list1 = angular.fromJson(data.substring(3));
//I can't call any function from here to use this variable
});
I am trying to access the updated list1 variable in the code below, but its value remains '' due to it being accessed before being updated - prior to receiving the response.
Is there a method to utilize the updated value of the list1 variable in the following code?
Edit 1
Following the recommendation from @Manish Singh in one of the responses, I attempted using $rootScope.watch. It successfully reflects the new value for the above code snippet but not for the subsequent code provided below.
$rootScope.peers = [];
industrysearch.searchindustry(indussymbol).then(function(results) {
industrysearch.searchpeers($scope.industrystock[0]._source.industry).then(function(results1) {
for (var number_stocks = 0; number_stocks < results1.length; number_stocks++) {
$rootScope.peers.push(results1[number_stocks]);
}
});
});
$rootScope.$watch('peers', function(newVal1, oldVal1) {
console.log(newVal1); //prints []
});
The only difference seems to be that one is a normal variable and the other is used as an array.
Edit 2
I missed using $watchCollection for arrays/collections. The following code is now functional.
$rootScope.$watchCollection('peers', function(newVal1, oldVal1) {
console.log(newVal1);
});
Thank you for the assistance!