AngularJS : How to watch service variables?

后端 未结 21 1641
盖世英雄少女心
盖世英雄少女心 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:26

    I've seen some terrible observer patterns here that cause memory leaks on large applications.

    I might be a little late but it's as simple as this.

    The watch function watches for reference changes (primitive types) if you want to watch something like array push simply use:

    someArray.push(someObj); someArray = someArray.splice(0);

    This will update the reference and update the watch from anywhere. Including a services getter method. Anything that's a primitive will be updated automatically.

提交回复
热议问题