admin管理员组文章数量:1294341
I have one condition in controller. If the condition return true, then I have to disable all links and ng-click
s
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-click
s
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?
6 Answers
Reset to default 8What 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
版权声明:本文标题:javascript - Angular Js :Conditionally how to disable all button and links? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741588991a2387009.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论