Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngModelOptions with Input in Angular form

I'm getting this suggestion in the developer-console when using angular template-driven form and not writing 'name' attribute but, I'm using [(ngModel)] with the input field.

control must be defined as 'standalone' in ngModelOptions.

 Example 1: <input [(ngModel)]="person.firstName" name="first">
 Example 2: <input [(ngModel)]="person.firstName" [ngModelOptions]="{standalone: true}">

If I'm using 'name' attribute or [ngModelOptions]="{standalone: true}" then working fine.

But I'm not sure why that suggestion is occuring?

like image 250
Nitish Avatar asked Jul 24 '26 13:07

Nitish


1 Answers

If i could see your whole form code,i could explain more clearly. 

 Assuming you are using [formGroup] directive,then 

This error could come if you are mentioning formControlName for only some fields in the form and not for all. Angular expects us to use formContorl in the reactive form and you are using ngModel . Since it cannot get an instance of formControl, its asking you to mention [ngModelOptions]="{standalone: true}". You can achieve same thing with formControl without ngModel binding for fields also.

    https://angular.io/guide/reactive-forms
like image 91
Nabin Kumar Khatiwada Avatar answered Jul 27 '26 01:07

Nabin Kumar Khatiwada



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!