Lit-element: if the value is undefined, then attribute being created

Created on 17 Sep 2018  路  5Comments  路  Source: Polymer/lit-element

do as it was, if the value is undefined, the attribute should not be created

version 0.6:
<a href="${item.link}" ></a> --> <a href="undefined"></a>

version 0.5.2:
<a href="${item.link}" ></a> --> <a></a>

Most helpful comment

@Leon-Alexey ok, the href is still attribute, so the proper solution would be to use directive:

import { ifDefined } from 'lit-html/directives/if-defined.js';

and then use it like this:

<a href="${ifDefined(item.link)}"></a>

See working demo here: https://glitch.com/edit/#!/linen-bonsai?path=index.html:17:4

All 5 comments

@Leon-Alexey please read the changelog and docs carefully before starting to complain.
The binding notation has been changed in 0.5.2 so you should now bind to a property:

<a .href="${item.link}"></a>
<a .href="${item.link}"></a>

It's not working

if (item.link !== undefined) { return html'<a href="${item.link}"></a>' } else { retun html'<a></a>' }

if need add 10 attributes then to make 10 'if'

@Leon-Alexey ok, the href is still attribute, so the proper solution would be to use directive:

import { ifDefined } from 'lit-html/directives/if-defined.js';

and then use it like this:

<a href="${ifDefined(item.link)}"></a>

See working demo here: https://glitch.com/edit/#!/linen-bonsai?path=index.html:17:4

works, Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

minht11 picture minht11  路  4Comments

PotterDai picture PotterDai  路  4Comments

chrismbeckett picture chrismbeckett  路  3Comments

ghost picture ghost  路  3Comments

mercmobily picture mercmobily  路  3Comments