disable the button after one click using ng-disabled

[亡魂溺海] 提交于 2020-01-07 03:49:42

问题


Below is the code for the ng-click, I want the click event only once and then disable the button. But, how to use ng-disabled for only button tag (without using input tag)?

Suggestion would be really appreciated for the beginner to 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>

回答1:


Have you tried:

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



回答2:


<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>


来源:https://stackoverflow.com/questions/43867482/disable-the-button-after-one-click-using-ng-disabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!