Ivan Beseda Ivan Beseda - 28 days ago 15
Javascript Question

Disabling ng-click on this element

I have three buttons in one controller with ng-click functions. How I can disable click when this element is already clicked?
Here is example:

<div ng-app="myApp">
<div ng-controller="GreetingController">
<button ng-click="!IsClickEnable||DoSomethingElse()">xyz</button>
<button ng-click="!IsClickEnable||DoSomethingElse()">xyz</button>
<button ng-click="!IsClickEnable||DoSomethingElse()">xyz</button>
</div>
</div>

var myApp = angular.module('myApp', []);

myApp.controller('GreetingController', ['$scope', function($scope) {
$scope.IsClickEnable = true;
$scope.DoSomethingElse = function() {
alert('click')
$scope.IsClickEnable = false;
};

}]);

Answer

var myApp = angular.module('myApp', []);

myApp.controller('GreetingController', ['$scope', function($scope) {
  $scope.DoSomethingElse = function(event) {
    event.target.disabled = true;
  };

}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="GreetingController">
    <button ng-click="DoSomethingElse($event)">xyz</button>
    <button ng-click="DoSomethingElse($event)">xyz</button>
    <button ng-click="DoSomethingElse($event)">xyz</button>
  </div>
</div>

Comments