null
removes it:
[attr.checked]="value ? '' : null"
or
[attr.checked]="value ? 'checked' : null"
Hint:
Attribute vs property
When the HTML element where you add this binding does not have a property with the name used in the binding (checked
in this case) and also no Angular component or directive is applied to the same element that has an @Input() checked;
, then [xxx]="..."
can not be used.
See also What is the difference between properties and attributes in HTML?
What to bind to when there is no such property
Alternatives are [style.xxx]="..."
, [attr.xxx]="..."
, [class.xxx]="..."
depending on what you try to accomplish.
Because <input>
only has a checked
attribute, but no checked
property [attr.checked]="..."
is the right way for this specific case.
Attributes can only handle string values
A common pitfall is also that for [attr.xxx]="..."
bindings the value (...
) is always stringified. Only properties and @Input()
s can receive other value types like boolean, number, object, ...
Most properties and attributes of elements are connected and have the same name.
Property-attribute connection
When bound to the attribute the property also only receives the stringified value from the attribute.
When bound to the property the property receives the value bound to it (boolean, number, object, ...) and the attribute again the stringified value.
Two cases where attribute and property names do not match.
Angular was changed since then and knows about these special cases and handles them so that you can bind to <label [for]="
even though no such property exists (same for colspan
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…