AngularJs ng-change 事件手动触发

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';
}

当我点击复选框时,我将值设置到文本框中,但我看到 ng-change 事件没有被触发。为什么?

而且当我在文本框中输入一些值时,我观察到 ng-change 事件被触发。

是否解决了这个问题?

根据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

所以被JavaScript(/angular)改变时不会触发。

你可以做的,是自己触发更改功能:

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

this jsfiddle