我为国家及其州创建了一个模块。所以在添加时,它工作正常,但在编辑时,它不会选择。
HTML代码
<div fxLayout="row" fxLayoutWrap="wrap">
<md-card-content class="ml-xs mr-xs" style="width: 100%">
<md-select placeholder="Country" [formControl]="formCompany.controls['CountryId']" [ngModel]="CompanyEdit.CountryId" #countryId (change)="getStateslist($event.value)">
<md-option *ngFor="let country of getCountries" [value]="country.id">{{country.CountryName}}</md-option>
</md-select>
</md-card-content>
<small *ngIf="formCompany.controls['CountryId'].hasError('required') && formCompany.controls['CountryId'].touched" class="mat-text-warn">Please select country.</small>
</div>
<div fxLayout="row" fxLayoutWrap="wrap">
<md-card-content class="ml-xs mr-xs" style="width: 100%">
<md-select *ngIf="getStates" placeholder="State" [formControl]="formCompany.controls['StateId']" [ngModel]="CompanyEdit.StateId">
<md-option *ngFor="let getState of getStates" [value]="getState.id"> {{getState.StateName}} </md-option>
</md-select>
</md-card-content>
<small *ngIf="formCompany.controls['StateId'].hasError('required') && formCompany.controls['StateId'].touched" class="mat-text-warn">Please select state.</small>
</div>
//组件文件代码
this.formCompany = this.fb.group({
StateId: [null,Validators.compose([Validators.required])],
CountryId: [null,Validators.compose([Validators.required])],
}});
开启编辑
if(this.route.snapshot.params['id']){
this.services.getStates(0).subscribe(res => {this.getStates = res});
this.services.getCompanyById(this.route.snapshot.params['id']).subscribe(res => {this.CompanyEdit = res } );
}