I’m trying to conditionally add a custom attribute to an element and couldn’t find a way to do it.
An example: Suppose we have a list of items (blog posts or whatever), and we want to use route-href
to link to the edit state for each item, but only if the item is not read-only.
If the item is editable, we want to have:
<a route-href="route: edit; params.bind: {itemId: item.id}">Some complex markup for that item</a>
But if it’s read-only, we want to have
<a>Some complex markup for that item</a>
Duplicating the element, once with route-href
and once without, and using if.bind
is not an option since that creates combinatorial explosion when there’s more than one conditional attribute.
In AngularJS this problem could be solved trivially with ng-attr-*
:
<a ng-attr-route-href="{{editable ? 'route: edit; params.bind: {itemId: item.id}' : undefined}}">Some complex markup for that item</a>
But as discussed in this templating feature request, Aurelia still has no builtin way to do this.
OK, fine, I’m willing do create a custom attribute or binding behavior to add the route-href
manually if the condition is met. The problem: I can’t find a way to attach a custom attribute to an existing element from inside another custom attribute.
There are plenty of other use-cases for this, eg., only adding a tooltip if a certain condition is met. Has anyone ever done this before or knows how to do it?