防止ng-click =“"对禁用按钮的影响 [英] Prevent ng-click="" effect on a disabled button

查看:35
本文介绍了防止ng-click =“"对禁用按钮的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止AngularJS不要单击具有禁用"属性的HTML按钮标记?

How can I prevent AngularJS to not click on an HTML button tag that has "disabled" attribute?

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<button type="button" disabled ng-click="something()">No Click Please!</button>

在这种情况下,不应调用something().

In this case, something() should NOT be called.

推荐答案

您传递的表达式必须在

You pass an expression that has to be evaluated in the ngClick directive. If it is not evaluated true then something() will not be called.

这里有个例子:

(function() {

  angular.module('MyApp', [])
    .controller('MyController', MyController);

  MyController.$inject = ["$scope"];

  function MyController($scope) {

    $scope.disabled = true;

  }

})();

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div data-ng-app="MyApp">

  <div data-ng-controller="MyController">

    <button type="button" ng-disabled="disabled" ng-click="disabled || something()">No Click Please!</button>

    <button type="button" data-ng-click="disabled = !disabled">{{disabled ? 'Enable' : 'Disable'}}</button>

  </div>

</div>

请注意,您还可以使用 ngDisabled 指令,以便$scope.disabled为true,将不会调用something(),并且该按钮也将被禁用!

Notice you can also use the ngDisabled directive so that if $scope.disabled is true, something() will not be called and the button will also be disabled!

这篇关于防止ng-click =“"对禁用按钮的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆