使用ng-disabled一次单击后禁用该按钮

时间:2017-05-09 10:34:56

标签: angularjs angularjs-ng-disabled

以下是ng-click的代码,我只想点击一次事件,然后禁用按钮。但是,如何仅对按钮标签使用ng-disabled(不使用输入标签)?

初学者对angularjs的建议非常感激。

 <html>
<script 
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<p>Click the button to run a function:</p>
 <button ng-click="!count && myFunc()">OK</button>
 <p>The button has been clicked {{count}} times.</p>
 </div>
 <script>
 angular.module('myApp', [])
 .controller('myCtrl', ['$scope', function($scope) {
 $scope.count = 0;
 $scope.myFunc = function() {
 $scope.count++;
 };
  }]);
  </script>
  </body>
  </html>

2 个答案:

答案 0 :(得分:2)

你试过了吗?

<button ng-click="!count && myFunc()" ng-disabled="count > 0">OK</button>

答案 1 :(得分:1)

<script>
 angular.module('myApp', [])
 .controller('myCtrl', ['$scope', function($scope) {
 $scope.count = 0;
 $scope.myFunc = function() {
 $scope.count++;
    if($scope.count > 1){
        angular.element('button').addClass('disableButton');
    }
 };
  }]);
  </script>

<style>
.disableButton{
Change color of button to #848d95
}
</style>