3

I am trying to use ng-attr to conditionally add an attribute to an element. My code is something like this:

<label for="theID" 
       ng-attr-disabled="{{true || undefined}}"
       class="control-label">
  Some Label
</label>

What I get by inspecting the element is this:

<label translate="" for="theID" 
       ng-attr-disabled="{{true || undefined}}" 
       class="control-label ng-scope" disabled="disabled">
  Some Label
</label>

But expectation is:

<label translate="" for="theID" 
       ng-attr-disabled="{{true || undefined}}" 
       class="control-label ng-scope"
       disabled>
  Some Label
</label>

am I wrong altogether about how it works?

Thanks

6
  • Try it without the curly quotes (though that's going to just evaluate to true, but whatever). Commented Apr 14, 2017 at 15:39
  • Then it looks like this: ng-attr-disabled="true || undefined" Commented Apr 14, 2017 at 15:42
  • Yes, but true || undefined will always evaluate to true. So why even bother with ng-attr? Why not just use disabled? Commented Apr 14, 2017 at 15:48
  • "true" is just to make a point. There will be a real expression. So, why "disabled" attribute is getting a value? Commented Apr 14, 2017 at 15:55
  • stackoverflow.com/a/6961821/3055401 Commented Apr 14, 2017 at 16:02

3 Answers 3

4

I don't think you should care about it, in your case. When value is falsy, disabled is not present. When value is truthy, is present - and it's all about presence in HTML about attributes like this (like required on inputs, validation care about presence required="false" makes input required). For example, you can style it dependent on attribute presence: MDN Attribute selectors.

Sign up to request clarification or add additional context in comments.

3 Comments

Read my comment, please.
But I need to get it without any value. Is that possible?
@Mark I think you can only do it with plain DOM operation: stackoverflow.com/a/18774169/3368498 . You can try label translate="" for="theID" id="label1" ng-init="$ctrl.addDisabled('label1')" and inside addDisabled(id) you can do $document.querySelector("#" + id).setAttribute("disabled", "")
3

Angular has a special directive for the disabled attribute:

<label for="theID" 
        ̶n̶g̶-̶a̶t̶t̶r̶-̶d̶i̶s̶a̶b̶l̶e̶d̶=̶"̶{̶{̶t̶r̶u̶e̶ ̶|̶|̶ ̶u̶n̶d̶e̶f̶i̶n̶e̶d̶}̶}̶"̶
        ng-disabled="true || undefined"
        class="control-label">
    Some Label
</label>

From the Docs:

ngDisabled

  • directive in module ng

This directive sets the disabled attribute on the element (typically a form control, e.g. input, button, select etc.) if the expression inside ngDisabled evaluates to truthy.

A special directive is necessary because we cannot use interpolation inside the disabled attribute. See the interpolation guide for more info.

— AngularJS ng-disabled Directive API Reference

Comments

0

If you want to disable element in Angular 4+ just add disabled attribute with your condition

<label for="theID" 
 disabled="condition"
 class="control-label">
    Some Label
</label>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.