I have a strange behavior in my app using AngularJS 1.5.8:
plunker (https://plnkr.co/edit/zaHVJeK8hdxk2gaOL9Pf?p=preview) and video(http://recordit.co/eszvfdfC9S)
- step 1. At the beginning changing
ng-requireddoesn't callng-changefunction - step 2. After making changes in
inputAND removing them (inputis empty)ng-requiredDOES callng-changefunction
expected behavior?
- step 2. After making changes in
inputAND removing them (inputis empty)ng-requiredSHOULD NOT callng-changefunction. As it was at the beginning, and as it is when input has some value
Please let me know if it's a bug or not. If not then why changing ng-required calls ng-change NOT always or even at all?
ANSWER IS FOUND-------------------------------------------------------------------------------------------------------
NgModelController has two properties: $viewValue (value entered by user) and $modelValue (value bound to your model). When the value entered by the user fails validation, NgModelController sets the model to undefined.
In AngularJS 1.3, they added the ng-model-options directive. It lets you configure how/when the model gets updated. You can use the allowInvalid option to prevent your model from being set to undefined:
ng-model-options="{allowInvalid: true}"