A bit of background, I want to create a service that does all the http fetching and this service will also flag the main controller about new set of data is here.
.factory('watcher',function($http,$rootScope,$interval){
var url="https://helloworld.com";
var get_data = function(){
$http.get(url).success(function(resp){
if(resp['result'].length>3){
window.localStorage['data'] = resp['result'];
$rootScope.flag = !($rootScope.flag);
}
});
};
return {
get_data: function(){
$interval(get_data,60*1000);
get_data();
}
};
})
.controller('helloCtrl',function($scope,$rootScope){
$scope.$watch('flag',function(newValue){
console.log("Local");
});
$rootScope.$watch('flag',function(newValue){
console.log("Global");
});
})
What I am expected here is; Global
will print on my console doesn't matter where I am on the navigation page, but 'Local' will only show when I go back to the page where helloCtrl
is declared.
What I am actually getting here is; Global
and Local
are printed on console, regardless where I am.
Thansks in advance