0

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

roger
  • 1,225
  • 2
  • 17
  • 33
  • Read [AngularJS Developer Guide - Scope Hierarchies](https://docs.angularjs.org/guide/scope#scope-hierarchies). – georgeawg Mar 13 '17 at 03:13
  • Possible duplicate of [What are the nuances of scope prototypal / prototypical inheritance in AngularJS?](http://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical-inheritance-in-angularjs) – georgeawg Mar 13 '17 at 03:15

0 Answers0