Conditionally adding data-attribute in Angular directive template

AngularjsAngularjs Directive

Angularjs Problem Overview


I'm working on the template for a directive. If a property in the scope is set to true, data-toggle="dropdown" should be appended to the element. If the variable is false, this data attribute should not render as an attribute of the element.

For example, if scope variable is true, the template should render:

<span data-toggle="dropdown"></span>

If false, the template should render:

<span></span>

What would the template look like to accomplish this?


For example, I know that I can use ng-class to conditionally include a class. If I want the template to render this:

<span class="dropdown"></span>

Then my template would look like this:

"<span ng-class="{ 'dropdown': isDropDown }"></span>

If scope variable isDropDown is false, then the template will simply render:

<span></span>

So there's a way in a template to conditionally add a class="dropdown". Is there a syntax for templates that allows me to conditionally add data-toggle="dropdown"?


One of the things I've tried for the template is:

"<span data-toggle="{ 'dropdown': isDropDown }"></span>

My thinking with the above template is that if the scope variable isDropDown is true, the value of data-toggle will be set to "dropdown". If isDropDown is false, then the value of data-toggle would simply be an empty string "". That doesn't seem to work though.

Angularjs Solutions


Solution 1 - Angularjs

I think a good way could be to use ng-attr- followed by the expression you want to evaluate. In your case it would be something like:

<span ng-attr-data-toggle="{{ isValueTrue ? 'toggle' : 'notToggle' }}"></span>

Here's a fiddle with an example.

Solution 2 - Angularjs

<span ng-attr-data-toggle="{{isTrue && 'dropdown' || undefined }}"></span>

will produce when isTrue=true : <span data-toggle="dropdown"></span>

and when isTrue=false : <span></span>

Solution 3 - Angularjs

At the moment, there is no angular directive that allows you to remove or add an attribute conditionally. You can do ng-switch around the span, one with that attr and another one without it.

<div ng-switch on="condition">
<span data-toggle="dropdown" ng-switch-when="value"></span>
<span ng-switch-default></span>
</div>

or

<span data-toggle="dropdown" ng-if="expression"></span>
<span ng-if="!expression"></span>

You can also create a directive for that same purpose (adding/removing attrs conditionally) but that would be a bit more complicated.

Additionally if what you want is manage the scope variable inside the directive you can pass it as another attribute.

Example:

<span data-toggle="dropdown" when="isDropDown"></span>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncoreView Question on Stackoverflow
Solution 1 - AngularjsAurelioView Answer on Stackoverflow
Solution 2 - AngularjsGarfiView Answer on Stackoverflow
Solution 3 - AngularjsWawyView Answer on Stackoverflow