admin管理员组

文章数量:1294341

I have one condition in controller. If the condition return true, then I have to disable all links and ng-clicks

How we can do :-

Controller : $scope.disableActions = true;

In HTML

<button type="button" ng-click="ignoreAndRedisplay()" ng-disabled="disableActions">OpenClick</button>

So by doing this I have to write everywhere ng-disabled="disableActions", it will be redundant , How we can improve this for n number of button and links?

I have one condition in controller. If the condition return true, then I have to disable all links and ng-clicks

How we can do :-

Controller : $scope.disableActions = true;

In HTML

<button type="button" ng-click="ignoreAndRedisplay()" ng-disabled="disableActions">OpenClick</button>

So by doing this I have to write everywhere ng-disabled="disableActions", it will be redundant , How we can improve this for n number of button and links?

Share Improve this question edited Apr 27, 2017 at 10:26 tanmay 7,9112 gold badges23 silver badges39 bronze badges asked Apr 27, 2017 at 10:19 Ravi UbanaRavi Ubana 3956 silver badges26 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 8

What I'll suggest you to do for such cases would be, just put those all buttons inside single fieldset and apply ng-disabled over that fieldset, the controls which are inside that field set will automatically get disabled if ng-disabled expression gets evaluated to true

<fieldset ng-disabled="disableActions">
   <button type="button" ng-click="ignoreAndRedisplay()">OpenClick</button>
   <button type="button" >Some Other button</button>
   <button type="button" >One more button</button>
   ...
</fieldset>

Demo Here


As in ment you OP asking for disabling anchor tag. Basically you can't disable anchor tag. You could try below hack to make it working.

fieldset[disabled] a { 
   pointer-events: none;
}

Plunker

You can try to create your own directive, but ng-disabled is the correct way, as JinsPeter said

Yeah you will have to write everywhere ng-disabled as you are writing those buttons So it the right way to do it. It is redundant or not.

ng-disabled it the right way to do it. It is reduntant or not.

If you have an n number of links and button, use this bad practice of add this disabled attribute using a

$watch('disableAction',function(value){
   if(value==true){ 
    //use a selector here to select all your a and button elts and add a 
    //disabled="disabled" attribute.
   }
   else {
   // remove that disabled attribute
   }
});

ng-disabled is best. $scope.disableActions can use multiple place and affect all place.

<script src="https://ajax.googleapis./ajax/libs/angularjs/1.6.3/angular.min.js"></script>
<body ng-app ng-init="disableActions = false">
<button ng-disabled="!disableActions">OpenClick</button>
<button ng-disabled="!disableActions">Second Click</button>
<button ng-disabled="!disableActions">Third Click</button>
<button type="button" ng-click="disableActions = !disableActions">Enable/Disable</button>
</body>

Putting ng-disabled on all of the links will cost performance issues but its the right way to do it. But if you don't want some UI changes you can do it right in the controller.

$scope.ignoreAndRedisplay = function () {
    if ($scope.disableActions) return;
    // your code here
}

本文标签: javascriptAngular Js Conditionally how to disable all button and linksStack Overflow