AngularJS : How to watch service variables?

后端 未结 21 1314
盖世英雄少女心
盖世英雄少女心 2020-11-22 09:12

I have a service, say:

factory(\'aService\', [\'$rootScope\', \'$resource\', function ($rootScope, $resource) {
  var service = {
    foo: []
  };

  return          


        
21条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 09:22

    For those like me just looking for a simple solution, this does almost exactly what you expect from using normal $watch in controllers. The only difference is, that it evaluates the string in it's javascript context and not on a specific scope. You'll have to inject $rootScope into your service, although it is only used to hook into the digest cycles properly.

    function watch(target, callback, deep) {
        $rootScope.$watch(function () {return eval(target);}, callback, deep);
    };
    

提交回复
热议问题