在 Angular 表单组中初始化下拉列表

Initialize dropdown in Angular Formgroup

我正在尝试使用 Angular FormGroup(我正在使用 Angular 4)为表单中的下拉控件设置默认值。下拉列表很好地填充了给定的列表,但默认值不是 set.Also 它没有在代码中显示任何错误以下是我正在使用的代码 --

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
        });
}

如果我做错了什么,请提出建议。任何帮助将不胜感激。

您可以尝试从您的模板中删除 [(ngModel)]="CountryID" 吗?根据 docs,我猜测必须使用其中一个约定(如果使用响应式形式则为 formControlName,如果使用模板驱动形式则为 ngModel),否则它可能会混淆 'accessor'.