Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 6 warning for using formControlName and ngModel

I recently upgraded the angular version to 6-rc. I got following warning

It looks like you're using ngModel on the same form field as formControlName. Support for using the ngModel input property and ngModelChange event with reactive form directives has been deprecated in Angular v6 and will be removed in Angular v7

For more information on this, see our API docs here: https://angular.io/api/forms/FormControlName#use-with-ngmodel

What does it say exactly? the link does not have any fragment for #use-with-ngmodel

I guess I need to remove ngModel and use formGroup as my data binding object.

like image 418
Akshay Avatar asked Apr 19 '18 10:04

Akshay


People also ask

Can we use ngModel and formControlName together in Angular?

Angular warning: It looks like you're using ngModel on the same form field as formControlName. Support for using the ngModel input property and ngModelChange event with reactive form directives has been deprecated in Angular v6 and will be removed in Angular v7.

Can we use ngModel with reactive forms?

You can use [(ngModel)] with Reactive forms. This will a completely different directive than the one that would be used without the formControlName . With reactive forms, it will be the FormControlNameDirective . Without the formControlName , the NgModel directive would be used.

What does [( ngModel )] mean?

ngModel is a directive which binds input, select and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during validations in a form. We can use ngModel with: input. text.


2 Answers

If you're looking for Angular 6 documentation right now then use https://next.angular.io

https://next.angular.io/api/forms/FormControlName#use-with-ngmodel

So you have 3 options:

  1. use Reactive forms

  2. use Template driven forms

  3. silence warning (not recommended)

    imports: [   ReactiveFormsModule.withConfig({warnOnNgModelWithFormControl: 'never'}); ] 
like image 142
yurzui Avatar answered Oct 05 '22 23:10

yurzui


Remove [(ngModel)] from every field within formGroup contains formControlName and set value in controller class as follows simply this.form.get('first').setValue('some value'); do not close or silence warnings explicitly

like image 39
Sohail Anwar Avatar answered Oct 05 '22 23:10

Sohail Anwar