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

1

13

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

Intrados answered 20/11, 2013 at 16:40 Comment(0)
S
9

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

Settle answered 20/11, 2013 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.