我正在尝试使用Angular FormGroup(我使用Angular 4)在表单中为下拉控件设置默认值。下拉列表与给定列表填充良好,但未设置默认值。还没有在代码中显示任何错误以下是我正在使用的代码 -
HTML代码
<div class="col-xs-2">
<label class="control-label" for="Country">Country</label>
<select formControlName="CountryID" [(ngModel)]="CountryID" class="form-control">
<option *ngFor="let country of Countries" [ngValue]="country.CountryID">{{country.CountryName}}</option>
</select>
</div>
打字稿代码
ngOnInit() {
this.Countries = [
{ "CountryID": 0, "CountryName": "Select Country" },
{ "CountryID": 1, "CountryName": "Country 1" },
{ "CountryID": 2, "CountryName": "Country 2" },
{ "CountryID": 3, "CountryName": "Country 3" },
{ "CountryID": 4, "CountryName": "Country 4" },
{ "CountryID": 5, "CountryName": "Country 5" }
];
this.inputProcessingForm = this._domBuilder.group({
CandidateBuilderAppId: [''],
FirstName: ['Amit'],
MiddleInitial: '',
LastName: '',
Gender: 'S',
DateOfBirth: new Date(),
SocialSecurityNumber: 0,
PersonalEmail: 'amanand@liventus.in',
CorpEmail: '',
HomePhone: '',
WorkPhone: '',
WorkCellPhone: '',
WorkFax: '',
WorkExtension: '',
RemoteFax: '',
Address1: '',
Address2: '',
CountryID: 0,
State: 0,
City: 0,
ZipCode: 0
});
}
如果我做错了,请建议。任何帮助将不胜感激。
答案 0 :(得分:0)
您可以尝试从模板中删除[(ngModel)]="CountryID"
吗?根据{{3}}的原因,我猜测其中一个约定(formControlName
如果使用反应形式,或ngModel
如果使用模板驱动形式)必须使用,否则可能会混淆'访问”。