【发布时间】:2019-04-11 20:34:20
【问题描述】:
我正在尝试在选择框中显示 Subtext,如下面的屏幕截图所示。
我的 .html 代码是:
<form [formGroup]="form">
<mat-select placeholder="country" [formControl]="country" >
<mat-select-trigger>
{{country.name}}
</mat-select-trigger>
<mat-option *ngFor="let country of countries" [value]="country">
{{country.name}}
<span class="example-additional-selection">
Staff ID: {{country.id}}
</span>
<span class="example-additional-selection">
Staff Name: {{country.firstname}}
</span>
<span class="example-additional-selection">
Staff Type: {{country.staff}}
</span>
</mat-option>
</mat-select>
</form>
我的 .ts 代码是:
@Component({
selector: 'select-custom-trigger-example',
templateUrl: 'select-custom-trigger-example.html',
styleUrls: ['select-custom-trigger-example.css'],
})
export class SelectCustomTriggerExample {
countries = [
{id: 1, name: "United States", staff: "Sales", firstname: "Mark"},
{id: 2, name: "Australia", staff: "Sales", firstname: "John"},
...
];
form: FormGroup;
constructor() {
this.form = new FormGroup({
country: new FormControl(null)
})
}
get country(): string {
return this.form ? this.form.get('country').value : '';
}
}
我可以在选择框中列出子文本,但无法显示所选值。
当我将静态文本放入 mat-select-trigger 标记内时,它可以工作,但是当我放入变量 {{country.name}} 时,出现以下错误
错误:无法读取 null 的属性“名称”
这里是 stackblitz 链接:https://stackblitz.com/edit/angular-wd3vba-34arlh 如果你能看看并告诉我哪里做错了,我将不胜感激。
谢谢。
【问题讨论】:
标签: angular typescript angular-material