Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bind a boolean to the existence of a attribute in Angular 2?

Is it possible to bind a boolean variable to the existence of a attribute?

 template: ...,
 host: {
   "[attr.disabled]": "disabled"
 }

This is rendered in the element as disabled="true" or disabled="false". But I want to use it in css with the [disabled] selector, that if the variable is false, the attribute doesn't exist and if true it does exist. Thanks in advance.

like image 430
janispritzkau Avatar asked Feb 07 '23 13:02

janispritzkau


1 Answers

For a boolean attribute to be removed the value needs to be null not false

 "[attr.disabled]": "disabled ? true : null"
like image 70
Günter Zöchbauer Avatar answered Feb 11 '23 17:02

Günter Zöchbauer