Optional bound attributes

Is it possible to bind attributes when it’s is !null or !undefined only?

Example: I have an input element with a pattern attribute.

<input pattern.bind="_pattern"/>

But when _pattern is null or empty, the resulting HTML looks like

<input pattern="null"/>

which is not the same as


My solution would be


but that feels wrong to me so I ask for better approaches.


1 Like

Aurelia generally, and .bind specifically, by default works with property, so null/undefined will be assigned as-is to the property pattern of an <input/> element. You can have the above effect if you tell Aurelia to treat it as an attribute binding, via either following syntax:

<input pattern.attr="_pattern"/>

<input pattern.bind="_pattern & attr"/>

is this au1 or au2? or both?
where is this documented?

1 Like

& attr is for both version. .attr is only for v2. In v1, you can have a look at it in this GH issue Support assignment of attribute litterals via interpolation · Issue #106 · aurelia/templating-binding · GitHub
The implementation in v2 is different with the code example there, and is also better.

For the doc, I think it’s not doc’d properly in both versions. Will add it :+1:


For some reason, this doesn’t work for me.

<input pattern.bind="definition.scheme.textRegex & attr"/>

still produces

<input pattern/>


Edit: Fixed. I had to reset the value to null before.

bind() {
        if (this.definition.scheme.textRegex?.length === 0) {
            this.definition.scheme.textRegex = null;

Glad you got it working. A simple repro here in case you need to verify anything https://gist.dumber.app/?gist=e90b288be1629f308a020e33982af478

1 Like