2
<input ng-model="example" required>

Is it possible to include or exclude the attribute "required" based on a condition?

1
  • Where does the condition come from? Maybe you should not generate the attribute on the server side? Also, what do you hope to achieve by doing this? Are you sure you actually need this custom attribute/need to conditionally remove it? Commented May 22, 2015 at 16:57

2 Answers 2

1

you can use

<input type="text" ng-required="isRequired"/>

where in your controller:

$scope.isRequired = true;

Just set your scope variable to whatever condition you want!

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

5 Comments

In my case there is no "=". It is an HTML5 attribute. So I was wondering if there is anyway that I can completely exclude it from the HTML based on a condition.
hmm... is there a reason why you don't want to use ng-required instead?
Actually I posted "required" just as an example. In my particular case it's a custom attribute and the presence of it whether the value of true or false affects the functionality. So I need to not include the attribute depending on the condition.
From what I understand, the ng-required directive should work in your case. It will set the form input to "required" if your condition is truthful, or it won't be required otherwise.
@Prabhu check a possible solution i presented below with examples. from what you have commented on so far, i think you are referring to an HTML5 empty attribute syntax like required or checked
0

Actually I posted "required" just as an example. In my particular case it's a custom attribute and the presence of it whether the value of true or false affects the functionality. So I need to not include the attribute depending on the condition.

if this is in reference to empty HTML5 attributes and removing the attribute itself to avoid a boolean true condition then:

Example from MDN docs element.removeAttribute() :

// <div id="div1" align="left" width="200px"> 
document.getElementById("div1").removeAttribute("align"); 
// now: <div id="div1" width="200px">

Then according to AngularJS docs on angular.element and this external blog post you can:

  • remove an attribute with: angular.element(DOMelement).removeAttr(attribute)
  • read an attribute with angular.element(DOMelement).attr(attribute)
  • set an attribute with angular.element(DOMelement).attr(attribute, value)

Note: all element references in Angular are always wrapped with jQuery or jqLite; they are never raw DOM references.

hope this helps - GL!


EDIT1: Here's an SO discussion about DOM manipulation and Angularjs here's another

EDIT2: Here's an external tutorial on how to create an AngularJS Directive for DOM manipulations (including Unit testing)

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.