AngularJS - Why is ng-change called before the model is updated?

折月煮酒 提交于 2019-12-30 05:59:47

问题


Please see the following plnkr: http://plnkr.co/edit/KZwIBX?p=preview

The first standard select works as expected(at least as far as I expected/anticipated), meaning that ng-change is called after the model is updated. But when I create a directive that has an isolate scope it would seem that ng-change is called before my model is updated. Obviously there is something I am misunderstanding.

Situation very similar to AngularJS scope updated after ng-change


回答1:


The problem with the example in the plnkr is that you are passing the change function, to the isolate scope as a binding to the parent scope by using change : '&'. By using the & the function will be evaluated as an expression in the context of the parent and it will be evaluated before model changed (if there is no binding to the model as a parameter)

There can be two ways to change the example in order to work:

  1. you can define your isolated scope in the directive with the change: '=' definition

  2. you can use it as it is but call the parent referenced function with a parameter. For the second example here is a great explanation: http://www.thinkster.io/pick/oEo8ZAXv27/angularjs-isolate-scope



来源:https://stackoverflow.com/questions/20101697/angularjs-why-is-ng-change-called-before-the-model-is-updated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!