Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 835
shuttj Avatar asked Nov 20 '13 16:11

shuttj


1 Answers

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

like image 122
Cornel Stefanache Avatar answered Sep 29 '22 14:09

Cornel Stefanache