javascript ng-disabled 在 div 标签中工作吗?如果没有,那为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26230554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does ng-disabled work in div tag? if no, then why?
提问by Balu
i have tried using ng-disabled in div tag, but it is not working. does ng-disabled will work on div tags? if yes then how?
我曾尝试在 div 标签中使用 ng-disabled,但它不起作用。ng-disabled 是否适用于 div 标签?如果是,那么如何?
<div ng-disabled="true">
<button>bbb</button>
</div>
回答by Stephen Friedrich
"ng-disabled" just adds or removes the "disabled" attribute on the element. However in HTML "disabled" on a div has no effect at all.
“ng-disabled”只是添加或删除元素上的“disabled”属性。然而,在 HTML 中“禁用”一个 div 根本没有效果。
So in a way "ng-disabled" is working, but it just does not make sense to use it on a div.
所以在某种程度上“ng-disabled”是有效的,但在 div 上使用它是没有意义的。
You can add "ng-disabled" to a fieldset though which will make input elements nested in it appear disabled.
您可以将“ng-disabled”添加到字段集,但这会使嵌套在其中的输入元素显示为禁用。
Another workaround might be to simulate the disabled effect by using css properties "opacity" and "pointer-events: none", see http://nerd.vasilis.nl/disable-html-elements-with-css-only/
另一种解决方法可能是通过使用 css 属性“opacity”和“pointer-events: none”来模拟禁用效果,请参阅http://nerd.vasilis.nl/disable-html-elements-with-css-only/
回答by Nitsan Baleli
there is a number of elements in HTML that takes the 'disabled' attribute in effect, DIV is not one of them.
HTML 中有许多元素采用了“禁用”属性,DIV 不是其中之一。
you can see what elements take that attribute, here
您可以在此处查看哪些元素具有该属性
these elements accept the 'disabled' attr:
这些元素接受“禁用”属性:
- button
- input
- select
- textarea
- optgroup
- option
- fieldset
- 按钮
- 输入
- 选择
- 文本区域
- 选择组
- 选项
- 字段集
回答by Lax
you can use the next directive:
您可以使用下一个指令:
//***********************************// //this directive is ng-disabled directive for all elements and not only for button //***********************************//
//************************************// //这个指令是所有的ng-disabled指令元素而不仅仅是按钮 //***************************************//
app.directive('disabledElement', function () {
return {
restrict: 'A',
scope: {
disabled: '@'
},
link: function (scope, element, attrs) {
scope.$parent.$watch(attrs.disabledElement, function (newVal) {
if (newVal)
$(element).css('pointerEvents', 'none');
else
$(element).css('pointerEvents', 'all');
});
}
}
});
and the html:
和 html:
<div ng-click="PreviewMobile()" data-disabled-element="toolbar_lock"></div>
回答by DebbieMiller
The usage of 'ng-disabled', as the dev guide of angular says, is only for an input tag.
正如 angular 的开发指南所说,'ng-disabled' 的用法仅适用于输入标签。
<INPUT ng-disabled="">
...
</INPUT>
回答by EternalHour
So from what I gather, you are trying to disable the div when the button is pushed? Otherwise you can just disable the button like this.
因此,从我收集的信息来看,您是否试图在按下按钮时禁用 div?否则,您可以像这样禁用按钮。
<div>
<button ng-disabled="true">bbb</button>
</div>
But I don't understand why because it cannot be enabled again.
但我不明白为什么,因为它无法再次启用。