我正在使用 AngularJS,在将 ng-blur 与 ng-readonly 混合使用时遇到了问题。即使 ng-readonly 设置为 true,ng-blur 仍然会触发(如果单击输入字段然后单击其他地方)。
在示例中,ng-blur 仍然会触发对计数器进行递增。
http://plnkr.co/edit/cMQeVf2yJPG58CxS0eiB?p=preview
在 app.js 中
$scope.counting = function(){
$scope.count++;
}
在 index.html 中
<input
ng-blur="counting()"
placeholder="sample"
ng-readonly="true"
>
问题 当 ng-readonly 条件为真时,如何禁用 ng-blur?
最佳答案
您可以简单地在 ng-blur
上使用逻辑 &&
仅当 ng-readonly
时调用 counting()
> 是假的
<input ng-init="checked=true"
ng-blur="!checked && counting()"
placeholder="sample"
ng-readonly="checked">
此外,您可以使用 ng-init
设置 ng-readonly
的值,或者将其绑定(bind)到复选框。
这是 plunkr
关于javascript - 当 ng-readonly 为真时,AngularJS 如何禁用 ng-blur?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31218083/