ANGULAR5 Error: Ngmodel cannot is used to register form controls with a parent formgroup directive

Source: Internet
Author: User
Tags snmp netconf



An error occurred while creating a form:









The reason is that the Formgroup directive is used in the outermost form, but in one of the input plugins below, the Ngmodel directive is used, but the FormControl directive or the Formcontrolname attribute is not added.


 
1 <form [formGroup]="form">
  2 <mat-form-field>
  3 <input matInput placeholder="IP(SNMP)" [formControl]="snmpIpCtrl" required>
  4 <mat-error *ngIf="snmpIpCtrl.hasError(‘required‘)">IP(SNMP) cannot be empty</mat-error>
  5 <mat-error *ngIf="snmpIpCtrl.hasError(‘pattern‘)">Please enter a valid IP address</mat-error>
  6 </mat-form-field>
  7
  8 <div>
  9 <p>Whether Netconf is supported</p>
10 <mat-radio-group [(ngModel)]="netconfFlag">
11 <mat-radio-button value="0" color="primary">support</mat-radio-button>
12 <mat-radio-button value="1" color="primary">not supported</mat-radio-button>
13 </mat-radio-group>
14 </div>
15 </form>





Workaround:



1. Add the FormControl directive or the Formcontrolname attribute to the INPUT element


 
1 <div>
2 <p>Whether Netconf is supported</p>
3 <mat-radio-group [(ngModel)]="netconfFlag" [formControl]="netconfFlagCtrl">
4 <mat-radio-button value="0" color="primary">support</mat-radio-button>
5 <mat-radio-button value="1" color="primary">not supported</mat-radio-button>
6 </mat-radio-group>
7 </div>


and define the FormControl for validation in the corresponding component.ts.






2. AddngModelOptions instruction


 
1 <div>
2 <p>Whether Netconf is supported</p>
3 <mat-radio-group [(ngModel)]="netconfFlag" [ngModelOptions]="{standalone: true}">
4 <mat-radio-button value="0" color="primary">support</mat-radio-button>
5 <mat-radio-button value="1" color="primary">not supported</mat-radio-button>
6 </mat-radio-group>
7 </div>





ANGULAR5 Error: Ngmodel cannot is used to register form controls with a parent formgroup directive


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.