Jonathan Miles Jonathan Miles - 6 days ago 5
AngularJS Question

How to add conditional attribute in Angular 2?

How can I conditionally add an element attribute e.g. the

checked
of a checkbox?

Previous versions of Angular had
NgAttr
and I think
NgChecked
which all seem to provide the functionality that I'm after. However, these attributes do not appear to exist in Angular 2 and I see no other way of providing this functionality.

Answer

null removes it:

[attr.checked]="value ? true : null"
Comments