AngularJs ng-change event fire manually
Asked Answered
T

1

6
 <input type="checkbox" value="" ng-model="filterPrivateDocCheckBox" ng-click="dl.filterPrivateDocument(filterPrivateDocCheckBox, $event)">
 <input st-search="target" class="input-sm form-control"  ng-model="dl.documentTarget" ng-change="dl.change()" />

function filterPrivateDocument(val) 
{
    if(val)
    this.documentTarget = 'Private';
}

When I clicked on checkBox I set the value into text box, but I saw ng-change event doesn't get fired. why?

And also When I type some value in text box I observe that ng-change event gets fired.

Any fix for this problem?

Therontheropod answered 19/4, 2016 at 7:5 Comment(3)
Can you provide a jsFiddle/Plunker where one can reproduce the problem?Jeffries
So what is your problem here? The things you defined here is how your code is supposed to be working. What do want to be precisely?Nador
What browser ???Staff
T
12

According to the docs:

The ngChange expression is only evaluated when a change in the input value causes a new value to be committed to the model.

It will not be evaluated:

  • if the value returned from the $parsers transformation pipeline has not changed
  • if the input has continued to be invalid since the model will stay null
  • if the model is changed programmatically and not by a change to the input value

So it will not be triggered when it is changed by JavaScript (/angular).

What you can do, is trigger the change function yourself:

function filterPrivateDocument(val) {
    if(val) {
        this.documentTarget = 'Private';
        this.change();
    }
}

See this jsfiddle

Trangtranquada answered 19/4, 2016 at 7:22 Comment(2)
Thanks for your comment-3 highlighted in BOLDTherontheropod
strangely the 3rd point is exactly what's happening for me. can't figure out why but updating the value in the controller is in turn firing the ngChange #46002776Phelan

© 2022 - 2024 — McMap. All rights reserved.