我有一个问题时,当模型编程改变角度的NG-改叫。
I have a problem when angular's ng-change is called when model is changed programmatically.
$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>
下面是code例如: http://plnkr.co/edit / R4MO86ihMrauHXhpCMxi?p = preVIEW
Here is code example: http://plnkr.co/edit/R4MO86ihMrauHXhpCMxi?p=preview
信息应该是空的,因为的sendMessage
不应该被调用。模型以编程方式改变了。
Message should be null, because sendMessage
shouldn't be called. Model is changed programmatically.
据文档,你是对的。
https://docs.angularjs.org/api/ng/directive/ngChange
但是这似乎是一个错误所造成的,其中事件挂钩的顺序
but this seems to be a bug caused by the order in which the events are hooked up
绕着它的最好办法 - 用诉诸JS处理程序(的onchange)
The best way round it - with resorting to js handler (onchange)
$scope.$watch("mySelectBox", function(a,b) {
if (a.name!=b.name) {
$scope.message = "Message sent! (old="+b.name+', new='+a.name+')';
}
});
请参阅普拉克 http://plnkr.co/edit/2ZbxS1tszppR9SrNqxVB?p=$p$pview
心连心