I have an angular service set up like -
function updateViewService($http, $interval) {
var change = true;
var grabTimeStamp = function(){
$http.get('someapi').then(function success(response){
//some logic to change var change according to the response
}
$interval(grabTimeStamp, 10000);
return { refresh: function(){
return change;
}};
}
Now what I need to do is return change in controller and also update the controller variable as soon as service change changes.
What I suspect is in the controller, the service return function returns the value only once. So I tried $interval in my controller while calling the service, that did not update the controller variable either.
I have a lot of controllers, that is why I made one service that should update all the controllers.
Then I tried $rootScope.$broadcast whenever change variable changes. Tried to listen to broadcasts in controller with $scope.$on but did not work.
if(change === true){
$rootScope.$broadcast('dataChanged',change);
change = false;
}
What should be the ideal way to do this?
Basically what I want to do is-
Create a service that makes a request every ten seconds and update the
changevariable (Done)Pass this variable to the controller and update the view (basically make a request to the back-end and get the new data {stuck})
Thanks.