NgModel not working on custom component in Angular

[(ngModel)] is not working on custom angular components

<input type="text" [(ngModel)]="term">Code language: HTML, XML (xml)

The above doesn’t work on custom components, but you can replace with the following:

<input type="text" [value]="term" (input)="term = $event.target.value">Code language: HTML, XML (xml)

or, an alternative without the need for a component property:

<input matInput type="text" [value]="term || ''" (input)="term = $event.target.value">
Code language: HTML, XML (xml)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.