質問

This post is not the same as: Is there a tidy way to define a large watch collection for AngularJS?

My code is (service.js):

var MyJSON = {
    array: [
        {k:'v'},
        {k:'v'},
        {k:'v'}
    ],
    info: {
        k: 'v',
        k2: 'v2'
    },
    last: 1398680914943 // Date.now()
}

$rootScope.$watchCollection(function () { return MyJSON; }, function (n, o) {
    console.log('Change');
});

Detects changes when I work on the root object "MyJSON". Like this:

MyJSON.last = 123456789; // console: Change

But if I am doing something like this:

MyJSON.info.k = 'vDummie';

or:

MyJSON.array.push({k:'something'});

"$watchCollection" does not work.

役に立ちましたか?

解決

$watchCollection watches only the 1st level properties; use $watch(..., ..., true) to do "deep" watching. Note: there are 3 arguments, the first two are the same as your code, the third is true for deep watch.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top