[英]Angular 6 cannot automatically select/bind dropdown list value from supplied object
我从服务中获取一个国家/地区对象,并将其绑定到具有国家/地区下拉列表的表单。 从服务中检索国家后,该国家似乎没有被选中,但所有其他数据都显示为OK,包括绑定到下拉列表的字符串性别字段。 我可以从列表中手动选择国家/地区。 如何自动将所选国家/地区显示?
个人details.component.html
<form [formGroup]="form" (submit)="doSave(form.values)">
<mat-card class="main-card">
<mat-card-content>
<p>Personal Details</p>
<mat-form-field class="full-width-input">
<input matInput placeholder="Given name" formControlName="givenName" >
</mat-form-field>
<mat-form-field class="full-width-input">
<input matInput placeholder="Surname" formControlName="surname" required>
</mat-form-field>
<mat-form-field class="full-width-input">
<mat-select placeholder="Select gender" formControlName="gender" required>
<mat-option *ngFor="let g of genders" [value]="g">{{g}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field class="full-width-input">
<mat-select placeholder="Select country of birth" formControlName="countryBorn" required>
<mat-option *ngFor="let country of countries" [value]="country">{{country.name}}</mat-option>
</mat-select>
</mat-form-field>
</mat-card-content>
</mat-card>
</form>
个人details.component.ts
countries = [
new Country('1100', 'Australia', '1'),
new Country('1201', 'New Zealand', '160'),
new Country('3105', 'Malta', '140')
];
genders = ['F', 'M'];
ngOnInit() {
this.form = this.fb.group({
givenName: ['', Validators.required],
surname: ['', Validators.required],
gender: ['', Validators.required],
countryBorn: ''
});
this.response = this.applicantService.getDetails()
.pipe(take(1), tap(resp => {
this.form.patchValue(resp.personalDetails);
}));
}
域:
export class PersonalDetails {
givenName: string;
surname: string;
gender: string;
countryBorn: Country;
constructor(givenName?: string, surname?: string, gender?: string, countryBorn?: string) {
this.id = id;
this.surname = surname;
this.gender = gender;
this.countryBorn = countryBorn;
}
}
export class Country {
id: string;
name: string;
displayOrder: string;
constructor(id?: string, name?: string, displayOrder?: string) {
this.id = id;
this.name = name;
this.displayOrder = displayOrder;
}
}
数据:
incoming value from service: { "surname": "Oliver", "givenName": "Bert", "gender": "M", "countryBorn": { "id": "1201", "name": "New Zealand", "displayOrder": "160" }, "birthdate": "1990-04-21" }
Form value on load ({{form.value | json }}) = { "givenName": "Bert", "surname": "Oliver", "gender": "M", "countryBorn": { "id": "1201", "name": "New Zealand", "displayOrder": "160" }
谢谢
Angular使用对象标识来选择选项,并提供特殊的输入属性来自定义默认选项比较算法: compareWith :
HTML
<mat-select ... formControlName="countryBorn" [compareWith]="compareFn">
^^^^^^^^^^^^^^^^^^^^^^^^^
TS
compareFn(c1: Country, c2: Country) {
return c1.id === c2.id;
}
将resp.personalDetails.countryBorn设置为从列表中的实际对象,然后将其分配给表单(因为对象比较{...} === {...}始终为false,Angular将无法匹配并选择它)
resp.personalDetails.countryBorn = this.countries.find(country=> resp.personalDetails.countryBorn.id);
或者将id单独设置为值
<mat-option *ngFor="let country of countries" [value]="country.id">{{country.name}}</mat-option>
数据只有国家ID而不是整个对象。
{ "surname": "Oliver", "givenName": "Bert", "gender": "M", "countryBorn": "1201", "name": "New Zealand", "displayOrder": "160" }, "birthdate": "1990-04-21" }
或者您可能需要使用compareWith定义您自己的比较函数: https : //angular.io/api/forms/SelectControlValueAccessor#caveat-option-selection
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.