I have a table created with ng-repeat and I want to execute some action after I change quantity from one value to another (and also it would be nice to have original value before the change). I tried using ng-change="myFunction(result, {{result.modelValue}})" but the problem is that this event is triggered by every keystroke and so I typed, say, 7 and actually want to type 70, but the event already happened. I also tried ng-blur, but then nothing happened at all (my function was not even called). Note, that I also have ng-click in the ng-repeat.
Do you know which event should I attempt to hook to and what is the best way to trace original value before the change (also, the complication is that I'm in the ng-repeat loop).
This is my current html (also, looks like it may have been a typo in my original code):
<td ng-if="result.isMatrix===false">
<div class="input-field-line-items">
<input type="number" name="quantity"
ng-model="result.quantity"
ng-disabled="crud.model.status===3"
class="form-control form-control-sm text-right"
ng-mouseleave="crud.quantityChanged(result)"
ng-class="{'input-has-error' : (result.quantity===null || lineItemForm.quantity.$error.max || lineItemForm.quantity.$error.min) && lineItemForm.quantity.$dirty}"
min="0"
max="2147483647" />
</div>
</td>
Based on the suggestions given I was almost able to get all working but I just found a case where my code doesn't work. This happens when I type bad value (say, -10). In this case I see an error in console. Here is my current html:
<div class="input-field-line-items">
<input type="number" name="quantity"
ng-model-options="{ updateOn: 'blur' }"
ng-model="result.quantity"
ng-disabled="crud.model.status===3"
class="form-control form-control-sm text-right"
ng-change="crud.quantityChanged(result, {{result.quantity}})"
ng-class="{'input-has-error' : (result.quantity===null || matrixLineItemForm.quantity.$error.max || matrixLineItemForm.quantity.$error.min) && matrixLineItemForm.quantity.$dirty}"
min="0"
max="2147483647" />
</div>
And this is the error I'm seeing after typing a negative value:
angular.js?v=hfDjvhqzZ7xKaOtdmnQ3FhY12ud2J6HVALMF-ee3YJo1:14525 Error: [$parse:syntax] Syntax Error: Token ')' not a primary expression at column 30 of the expression [crud.quantityChanged(result, )] starting at [)]. http://errors.angularjs.org/1.6.4/$parse/syntax?p0=)&p1=not%20a%20primary%20expression&p2=30&p3=crud.quantityChanged(result%2C%20)&p4=)
What should I change in the above to fix this issue?
I found the following thread which seems relevant but a bit above my head and I'd rather not introduce new directives into this mix if possible:
How to use the last valid modelValue if a model becomes invalid?