Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs ng-change event fire manually

 <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?

like image 959
shreyansh Avatar asked Apr 19 '16 07:04

shreyansh


1 Answers

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

like image 52
devqon Avatar answered Oct 12 '22 15:10

devqon