Matiishyn Matiishyn - 10 days ago 5
Javascript Question

Changing `ng-required` calls `ng-change`

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-required
    doesn't call
    ng-change
    function

  • step 2. After making changes in
    input
    AND removing them (
    input
    is empty)
    ng-required
    DOES call
    ng-change
    function



expected behavior?


  • step 2. After making changes in
    input
    AND removing them (
    input
    is empty)
    ng-required
    SHOULD NOT call
    ng-change
    function. 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

You should add

        ng-model-options="{allowInvalid: true}"

So the final result will be

<input type="text" 
    ng-change="$ctrl.onChange()" 
    ng-required="$ctrl.isRequired"
    ng-model-options="{allowInvalid: true}"
    ng-model="$ctrl.val"
    />
Comments