Reputation: 4270
I have a checkbox that needs to be shown as selected depending on a function result instead of binding it to an object property.
This would be easy, but is not possible:
<input type="checkbox" ([ngModel])="category.selected">
And this does not work, as even checked="false"
results in a checkbox being displshownayed as selected:
<input type="checkbox" [attr.checked]="isCategorySelected(category.id)"/>
I need an outcome like this
<input type="checkbox">
<input type="checkbox" checked>
depending on the result isCategorySelected(id)
.
Any help is appreciated.
Upvotes: 3
Views: 3034
Reputation: 988
You cannot use [(ngModel)]
in this scenario because it defines a 2-way databind, and you are passing a function.
Instead, you could simple use [ngModel]
, like the snippet below. This syntax defines a one-way databind.
<input type="checkbox" [ngModel]="yourBooleanFunction()"/>
You can read more about ngModel
here.
Let me know if you have any other problem with it.
Upvotes: 5