Javascript 在 angularjs 中,我们有 ng-disabled 指令,为什么框架不提供 ng-enabled 指令,因为我们有 ng-show 和 ng-hide
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30394447/
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
In angularjs we are having ng-disabled directive, why ng-enabled directive is not provided by the framework as we are having ng-show and ng-hide
提问by Avinash Solanki
In AngularJs ng-enableddirective is not provided. Is there any proper reason to not providing that directive in the framework, because we are having both ng-showand ng-hidewhen you can just use ng-hideto achieve our goal.
在 AngularJsng-enabled中没有提供指令。是否有任何适当的理由不在框架中提供该指令,因为我们同时拥有这两个指令,ng-show并且ng-hide您何时可以使用它ng-hide来实现我们的目标。
It wouldn't be nice just to check
ng-enabled="attribute.value === true"
只是检查一下可不好
ng-enabled="attribute.value === true"
instead of
ng-disabled="!(attribute.value === true)"
代替
ng-disabled="!(attribute.value === true)"
it will increase the readability of the code.
它将增加代码的可读性。
采纳答案by dfsq
The reason why there is no ngEnableddirective in Angular is rather semantical - there is simply nothing corresponding to it in HTML specification. At the same time there is already ngDisableddirective that works with disabledattribute. For the same reason, there is no ngUncheckeddirective, because there is already ngCheckedthat sets/removes checkedattribute.
ngEnabledAngular 中没有指令的原因是语义上的 - 在 HTML 规范中根本没有与之对应的内容。同时,已经有ngDisabled使用disabled属性的指令。出于同样的原因,没有ngUnchecked指令,因为已经ngChecked有设置/删除checked属性。
Now, the reasonable question: why we have both ngShowand ngHidethen? Well it's just for convenience in this case I guess, because having both ngShowand ngHideis not more confusing than ngShowalone, but at the same time it's very handy to have both.
现在,合理的问题:为什么我们有两个ngShow和ngHide呢?好吧,我想在这种情况下只是为了方便起见,因为同时拥有ngShow和ngHide并不比ngShow单独使用更令人困惑,但同时拥有两者都非常方便。
回答by rmuller
I am not missing an ng-enabled directive at all and I think it would add little to nothing to the framework.
我根本没有错过启用 ng 的指令,我认为它对框架几乎没有增加。
Inputs are enabled by default and HTML inputs also do not have an enabled attribute, just a disabled. The angular directive sets the HTML disabled attribute, but after evaluating an expression.
默认情况下,输入是启用的,HTML 输入也没有启用属性,只有禁用。angular 指令设置 HTML disabled 属性,但在评估表达式之后。
You can just write
你可以写
ng-disabled="!attribute.value"
ng-disabled="!attribute.value"
I think it is pretty readable.
我认为它的可读性很强。
回答by David Salamon
TLDR: Use angular-enabledinstead.
TLDR:改为使用angular-enabled。
The core team expressed their view in this this comment: https://github.com/angular/angular.js/issues/1252#issuecomment-49261373
核心团队在此评论中表达了他们的观点:https: //github.com/angular/angular.js/issues/1252#issuecomment-49261373
They will not abide a feature request just because it has many +1-s in order to keep the core bloat free.
他们不会仅仅因为一个功能请求有很多 +1-s 来保持核心膨胀自由。
However, if you still want to have ng-enabled functionality, btford has created this handy little module just for you: https://github.com/btford/angular-enabled
但是,如果您仍然想要启用 ng 的功能,btford 为您创建了这个方便的小模块:https: //github.com/btford/angular-enabled
回答by adamjld
Angular sets the disabled attribute based on the result of the expression in ng-disabled. There is no enabled attribute in HTML5 so ng-Enabled wouldn't work.
Angular 根据 ng-disabled 中的表达式结果设置 disabled 属性。HTML5 中没有 enabled 属性,因此 ng-Enabled 不起作用。
回答by Sourav Ghadai
This line worked for me.
这条线对我有用。
ng-disabled="!attribute.value"
回答by jusopi
Not that this is an answer to the question of Whybut for those who want to write their own directive, here you go. BTW it's in coffeescript.
并不是说这是对Why问题的回答,但对于那些想要编写自己的指令的人来说,就在这里。顺便说一句,它是在coffeescript。
.directive 'ngEnabled', [
'$parse'
($parse)->
dir =
restrict: 'AC'
link: ($scope, elem, attrs)->
getter = $parse attrs.ngEnabled
$off = $scope.$watch ->
getter $scope
, (val)->
elem.attr 'disabled', !val
$scope.$on '$destroy', -> $off()
]

