C
C
coderisimo2015-12-22 13:49:32
JavaScript
coderisimo, 2015-12-22 13:49:32

Why doesn't watch work in angular.js?

there is this piece of code:

$scope.edit_node = function (scope) { // когда приступаю к редактированию  подчиненного узла
                        $scope.singleData = scope.$modelValue; // добавляю редактируемый узел
                        $scope.singleData.parent_scope = scope.$parentNodeScope; // добавляю его родителя
                      };

// при любом изменении родителя хочу знать об этом
                    $scope.$watch('singleData.parent_scope', function (val) {
                        console.log('parent_scope updated!');
                    });

the bottom line is that when I change one of the parent's children $watch is not responsive. Question: why? After all, descendants are part of the parent, and when I change them, the parent also indirectly changes.
Thank you

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
bromzh, 2015-12-22
@bromzh

Try adding a 3rd parameter with value true to the $watch call.
UPD
Maybe just send notifications when there is a change? In the edit_node function, make $scope.$emit / $scope.$broadcast (depending on where in the hierarchy you need to send them).

C
coderisimo, 2015-12-22
@coderisimo

Of course I tried!
There the nesting is large (a bunch of objects that refer to other objects) an error pops up.
in the end, he began to follow not the whole singleData.parent_scope , but singleData.parent_scope.$childNodesScope.$modelValue.length' . seems to work)

S
Sergey, 2015-12-22
Protko @Fesor

if you change the data in the code - just notify in the place of the change that you have changed something. If you change through the UI - there is ng-changed. deep watch is a mega-castle that reduces control and increases the likelihood of problems in the future.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question