javascript - 当 ng-readonly 为真时,AngularJS 如何禁用 ng-blur?

标签 javascript angularjs

我正在使用 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/

相关文章:

javascript - 在javascript中解压缩gzip和zlib字符串

angularjs - npm/yeoman 安装 generator-gulp-angular 而不需要 sudo

angularjs - 与 $location.search() : how to remove a parameter from the url when it's null

html - BUG - IOS 中选择标签没有完成按钮

javascript - 编辑表单不更新

javascript - 未捕获的类型错误 : Cannot set property 'color' of undefined

javascript - 如何知道 Draft.js 插件何时准备就绪?

javascript - 根据事件更换类(class)

javascript - AngularJS + simpleCart 问题显示购物车内容直到添加项目

javascript - Angular 部分不会将编辑传播到应用程序