Developer Snippet Diary

[ngModel] vs [(ngModel)] in angular 15+

[ngModel]="fieldname": One-way data binding (property binding)
[(ngModel)]="fieldname": Two-way data binding (property binding and event binding)

 

[ngModel]="fieldname": This syntax is for one-way data binding. It binds the fieldname property from the component to the input element in the template. This means that changes in the input element will update the fieldname property, but not the other way around. It is also called property binding.

[(ngModel)]="fieldname": This syntax is for two-way data binding. It binds the fieldname property from the component to the input element in the template and vice versa. This means that changes in the input element will update the fieldnameproperty, and any changes in the property will also update the input element. The [(ngModel)] syntax is a combination of property binding (using square brackets) and event binding (using parentheses), which is why it is also known as "banana in a box" syntax.

In view

<input type="email" name="email" [(ngModel)]="fieldname">

in component

  fieldname:string='all';
Posted by: R GONDAL
Email: [email protected]