In angularjs we have the ng-disabled directive, why the ng-enabled directive is not provided by the framework, since we have ng-show and ng-hide

AngularJs directive does not specify ng-enabled . Is there any good reason not to provide this directive in the framework, because we have both ng-show and ng-hide , when you can just use ng-hide to achieve our goal.

It would be nice to just check ng-enabled="attribute.value === true"

instead of ng-disabled="!(attribute.value === true)"

this will increase code readability.

+45
javascript angularjs
May 22 '15 at
source share
6 answers

The reason the ngEnabled directive is ngEnabled in Angular is rather semantic - there is nothing in the HTML specification that matches it. At the same time, the ngDisabled directive already exists, which works with the disabled attribute. For the same reason, there is no ngUnchecked directive, because ngUnchecked already exists that sets / removes the checked attribute.

Now a reasonable question: why do we have both ngShow and ngHide ? Well, it's just for convenience in this case, I think, because having both ngShow and ngHide no more confusing than ngShow , but at the same time it is very convenient to have both.

+43
May 22 '15 at 10:41
source share

I do not miss the directive with ng support, and I think this will not add anything to the structure.

Inputs are enabled by default, and HTML inputs also do not have an activated attribute, but only disabled. The angular directive sets the attribute of disabled HTML, but after evaluating the expression.

You can just write

ng disabled = "! Attribute.value"

I think this is pretty readable.

+10
May 22 '15 at 10:41
source share

Angular sets a disabled attribute based on the result of the expression in ng-disabled. HTML5 does not have an activated attribute, so ng-Enabled will not work.

+4
May 22 '15 at 10:39
source share

TL; DR: use angular-enabled instead.

The core team expressed their opinion in this comment: https://github.com/angular/angular.js/issues/1252#issuecomment-49261373

They will not execute a function request just because it has many + 1-seconds to prevent the kernel from being released.

However, if you still want to use the ng-enabled functions, btford created this handy little module just for you: https://github.com/btford/angular-enabled

+3
Jul 14 '16 at 2:36
source share

Not that it was the answer to the question “Why,” but for those who want to write their own directive, here you go. BTW is in 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() ] 

http://plnkr.co/edit/F4RG2v859oFtTumvgoGN?p=preview

+1
Oct 07 '15 at 3:02
source share

It worked for me. Try it.

  ng-disabled="!(attribute.value)" 
0
Jun 20 '17 at 12:42 on
source share



All Articles