当以编程方式更改模型时调用 Angular 的 ng-change 时,我遇到问题。
$scope.sendMessage = function() {
$scope.message = "Message sent";
}
$scope.confirmed = true;
$scope.mySelectBox = $scope.selects[1];
<select ng-model="mySelectBox"
ng-options="item.name for item in selects track by item.name"
ng-change="sendMessage()">
</select>
这是代码示例:http://plnkr.co/edit/R4MO86ihMrauHXhpCMxi?p=preview http://plnkr.co/edit/R4MO86ihMrauHXhpCMxi?p=preview
消息应该为空,因为sendMessage
不应该被称为。模型以编程方式更改。
你可以尝试使用ng模型选项 https://docs.angularjs.org/api/ng/directive/ngModelOptions。请参阅此 plunker 以供参考http://plnkr.co/edit/BdKx62RW5Ls2Iz1H3VR1?p=preview http://plnkr.co/edit/BdKx62RW5Ls2Iz1H3VR1?p=preview.
在我的例子中我使用了ng-model-options="{ updateOn: 'change', debounce: { change: 0 } }"
这似乎有效。当我更改选择时,它仅运行 ngChange 中提供的函数。在初始化阶段message
保持空状态。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)