I’m encountering an interesting behavior in my Angular application, and I’m hoping someone can shed some light on it. Here’s the scenario:
I have a working code snippet where I define a property called isSelection like this:
public isSelection = model.required<boolean>();
public getTypeof(value: any): string {
return typeof value;
}
The isSelection property is initialized with a model.required signal
<input type="checkbox" [(ngModel)]="isSelection" />
<p>{{ getTypeof(isSelection) }}</p>
I bind the isSelection property to a checkbox input using [(ngModel)]
The paragraph displays the type of isSelection.
In this case the isSelection is displayed as a function when i press the checkbox the type of the isSelection never changed.
However, when I change the property declaration to:
public isSelection = input.required<boolean>();
The behavior changes, and the type of isSelection seems to switch from a function to a boolean.
NB I'm using the last version of angular 17.3.1.
input.required<boolean>()-> meant for one way binding, but you are using for two way binding, so the function gets replaced by a value that isbooleanTo remedy this, we can use one way binding in HTML
When I use the code provided as a child and provide inputs from the parent, angular just throws an error when I try to use
[(ngModel)]="isSelection2"model.required<boolean>()-> meant for two way binding, so your example works perfectly, since it does exactly what it's supposed to do!See the below code/Stackblitz:
child
main.ts
Working Stackblitz