javascript - AngularJS dependancies of variables and $watch -


I am trying to understand how anglo works and does not understand why the $ watch works I do not do it when the variable seen in the value is specified, I've boiled it in this test:

  & lt; Div ng-controller = "myController" & gt; TestElem = {{testElem}}, TestData [2] = {{TestData [2]}} & lt; / Div & gt;  , 8]; $ Scope $ apply () .; }; Set timeout ($ scope.debugFunc, 1000); $ Scope.testData = [1,2,3]; $ Scope.testElem = 0; $ Scope $ Watch ('$ scope.testData', function () {$ scope.testElem = $ scope.testData [2];}, true); }  

This output is testElem = 3, testData [2] = 3 then testElem = 3, testData [2] = 6 < / P>

I have been observed that at least once the clock function is performed after testElem == 3, but why not later the performance of TestData [] after each other?

use

  $ scope $ Watch ('testData', function () {$ scope.testElem = $ scope.testData [2];});  

You do not even need to type in the $ scope inside $ watch function

because you have numbers By looking at an array filled with (not filled, not context), you can leave the third parameter in the $ watch function


Comments