在Angular Formgroup中初始化下拉列表
我正在尝试使用Angular FormGroup(我使用Angular 4)为表单中的下拉控件设置默认值。下拉列表可以很好地填充给定的列表,但是没有设置默认值。另外,在下面的代码中没有显示任何错误,这就是我正在使用的代码-- HTML代码在Angular Formgroup中初始化下拉列表,angular,angular2-forms,Angular,Angular2 Forms,我正在尝试使用Angular FormGroup(我使用Angular 4)为表单中的下拉控件设置默认值。下拉列表可以很好地填充给定的列表,但是没有设置默认值。另外,在下面的代码中没有显示任何错误,这就是我正在使用的代码-- HTML代码 <div class="col-xs-2"> <label class="control-label" for="Country">Country</label> <select formCon
<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>
如果我做错了什么,请提出建议。任何帮助都将不胜感激。您能否尝试从模板中删除
[(ngModel)]=“CountryID”
?原因根据,我猜必须使用其中一种约定(formControlName
如果使用被动表单,或者ngModel
如果使用模板驱动表单),否则可能会混淆“访问者”
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
});
}