How does AngularJS distinguish between the following actions?

Let's assume this.model === $scope.model

.

The following will trigger a digest:

$scope.model.property1.property2 = true; 

      

but it won't be without a watch:

this.model.property1.property2 = true;  

      

How does AngularJS distinguish between these two lines of code?

+3


source to share


1 answer


I will do my best to answer this question. There is nothing that can lead to digestion from any of these lines:

$scope.model.property1.property2 = true;
this.model.property1.property2 = true;

      

Both are simple assignments. Digests are run from code that explicitly calls it. This usually happens either through $ scope. $ Eval (), $ scope. $ Apply () and explicit $ scope. $ Digest () calls. For example, ng-click binds to the elements of the click event and calls $ scope. $ Apply () with a function attached to this directive.

The $ digest loop first executes the expressions for evaluation in the asyncQueue scope (filled with $ scope. $ EvalAsync (), for example). It then traverses the areas and estimates which clock to run and calls the appropriate functions assigned to them (if it finds a change).



So, a couple indicates what to take away, which is related to your question:

  • Both lines above will not call $ digest
  • Angular doesn't need to distinguish between the two lines of code, because they are both simple assignments to the same property (assuming this.model === $ scope.model, as the question pointed out). Even if this.model and $ scope.model were different, this is still just an assignment, but for different purposes.
  • Watches "fire" in the $ digest loop when regions are traversed.

jsfiddle demonstrates this from a high level. Note that there is no explicit clock overlaid on $ scope.model.property1.property2 or this.model.property1.property2. The view is updated as both assignments are made in separate ng clicks that start the $ digest cycle. {{Model.property1.property2}} in the view sets the clock to this value, which is updated every ng click when $ digest occurs.

Without seeing the sample code where the real differences occur, I cannot tell why it behaves differently than the other.

+1


source







All Articles