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

shuttj picture shuttj · Nov 20, 2013 · Viewed 11.2k times · Source

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

Answer

Cornel Stefanache picture Cornel Stefanache · Nov 20, 2013

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