<input ng-model="example" required>
Is it possible to include or exclude the attribute "required" based on a condition?
<input ng-model="example" required>
Is it possible to include or exclude the attribute "required" based on a condition?
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!
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:
angular.element(DOMelement).removeAttr(attribute)
angular.element(DOMelement).attr(attribute)
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!
EDIT2: Here's an external tutorial on how to create an AngularJS Directive for DOM manipulations (including Unit testing)