Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally Add Attribute

Tags:

aurelia

Is it possible to conditionally add an attribute to an element using binding syntax? I am aware of if.bind, but that targets elements. Rather I am interested in targeting a specific attribute on an element.

Example:

<a href.bind="model.link">${model.text}</a>

If model.link is falsy, then I don't want the href at all--just treat the <a /> as a container element.

I realize I could create two <a /> tags--one with the attribute and one without--and use an if.bind on both, but that seem clunky and un-aurelia like.

like image 863
Kenneth K. Avatar asked Mar 08 '23 01:03

Kenneth K.


1 Answers

I don't think it's supported in Aurelia currently (issue 1, issue 2)

This,

<a href.bind="addLink ? link : ''">Link</a>. 

will produce

<a href>Link</a>

if addLink is false.

It won't remove the attribute entirely. If you are using a library which will check the existence of an attribute to manipulate the element, then this won't work. Another option would be to create a custom attribute like this. But that seems like an overhead.

like image 103
adiga Avatar answered Apr 05 '23 11:04

adiga