我是 Angular 4 的新手。我在这里陷入了困境。我收到如下错误。
html:26 ERROR Error:
ngModel cannot be used to register form controls with a parent formGroup directive. Try using
formGroup's partner directive "formControlName" instead. Example:
<div [formGroup]="myGroup">
<input formControlName="firstName">
</div>
In your class:
this.myGroup = new FormGroup({
firstName: new FormControl()
});
Or, if you'd like to avoid registering this form control, indicate that it's standalone in ngModelOptions:
Example:
<div [formGroup]="myGroup">
<input formControlName="firstName">
<input [(ngModel)]="showMoreControls" [ngModelOptions]="{standalone: true}">
</div>
at Function.webpackJsonp.../../../forms/@angular/forms.es5.js.TemplateDrivenErrors.modelParentException (forms.es5.js:4103)
at NgModel.webpackJsonp.../../../forms/@angular/forms.es5.js.NgModel._checkParentType (forms.es5.js:4423)
at NgModel.webpackJsonp.../../../forms/@angular/forms.es5.js.NgModel._checkForErrors (forms.es5.js:4410)
at NgModel.webpackJsonp.../../../forms/@angular/forms.es5.js.NgModel.ngOnChanges (forms.es5.js:4317)
at checkAndUpdateDirectiveInline (core.es5.js:10845)
at checkAndUpdateNodeInline (core.es5.js:12349)
at checkAndUpdateNode (core.es5.js:12288)
at debugCheckAndUpdateNode (core.es5.js:13149)
at debugCheckDirectivesFn (core.es5.js:13090)
我在这里遵循的代码是
HTML
<form class="form-horizontal" [formGroup]="addCargoForm" (ngSubmit)="registerUser(addCargoForm)" novalidate>
<div class="form-group col-md-4 col-sm-5 add-cargo-selector">
<label>Select Departure Country</label>
<select
class="form-control"
[(ngModel)]="selectedCountry"
(ngModelChange)="setCountry(selectedCountry)"
formControlName = "addCargoCountry">
<option value="" disabled selected>Select Country</option>
<option value="OMN">Oman</option>
<option value="QTR">Qatar</option>
<option value="AUH">Abhudhabi</option>
<option value="SJH">Sharjah</option>
<option value="SDI">Saudi</option>
<option value="DXB">Dubai</option>
</select>
<control-messages [control]="addCargoForm.controls.addCargoCountry"></control-messages>
</div>
</form>
在 Typescript 文件中我正在执行此操作已导入
import { Component } from '@angular/core';
import { NgbdModalBasic } from './modal-basic';
import { CommonModule } from '@angular/common';
import { FormsModule, FormBuilder, Validators } from '@angular/forms';
export class addYourCargo {
public addCargoForm = this.addCargoFB.group({
addCargoCountry: [""]
})
constructor(
public addCargoFB: FormBuilder){
}
}
实际上我在早期的组件中做了类似的表格,它工作正常我删除了 ngmodel、ngmodelChange 并且仍然显示相同的错误。
尝试了错误中给出的独立选项,但仍然显示相同的错误。
我在这里错过了什么吗。请帮忙,谢谢