Matt Westlake
Matt Westlake

Reputation: 3651

ng-change using previous value

I have the following input

<input style="margin-left:24px;" id="numOfRows" class="setupInput" size="10"
    ng-model="rowVal" ng-init="rowVal = gameCtrl.getNumOfRows()" ng-change="gameCtrl.setNumOfRows({{rowVal}})">

The issue I'm running into is when I change the value in the box it's always 1 behind.

EX: current value = 6

Why is it lagging 1 update behind?

Upvotes: 2

Views: 213

Answers (1)

PSL
PSL

Reputation: 123739

You may have couple of issues here:

1) Do not use interpolation ({{}}) in the ng-change expression. i.e Change

ng-change="gameCtrl.setNumOfRows({{rowVal}})"

to

ng-change="gameCtrl.setNumOfRows(rowVal)"

2) Do not use ng-init for initialization, that is what controller is for, use your controller to initialize the value of rowVal.

3) Make sure you do not have old version of angular which had issues with ng-change/ng-model syncronization.

4) Assuming you are using controller As syntax (and previous statement is wrong) always try to bind properties to the controller instance, i.e use gameCtrl.rowVal. (It could be wrong because it is not clear from the context of the question whether it really should belong to the controller instance or is it inside ng-repeat or something the created the child scope).

Upvotes: 4

Related Questions