user93 user93 - 2 months ago 18
AngularJS Question

not value after changes from angularjs $watch

In my angularjs directive I have used the following code to detect changes in this and execute function as follows

$scope.$watch(angular.bind(this, function () {
return this.dat;
}), function (newVal) {
})


and In my main controller I am calling the directive as follows

<line dat=ctrl.testdata></line>


Inside my main controller I have defined ctrl.testdata as follows

self.testdata = {};
self.testdata.dat = [ {'sdfds':'bbbb','sdfsd','lll'}]
self.testdata.k = {
"c": ["0y", "w6"],
"rs": ["n", "i"]
}


the newVal is changing when testdata.dat is changing but it is not changing when testdata.k is changing how do I proceed thanks in advance.

Answer

by default watch check only object equality - your test data same, when you edit it's props; You can use another binding, keeping this in mind, or use deep equality(poor performance);

$scope.$watch(angular.bind(this, function () {
                    return this.dat;
                }), function (newVal) {
         }, true)