【发布时间】:2020-03-09 07:45:21
【问题描述】:
我需要从 Angular 材质多选选项中同时选择 value and its text。我在下面解释我的代码。
<mat-form-field appearance="outline">
<mat-label>Grade</mat-label>
<mat-select
placeholder="Grade"
aria-label="Grade"
formControlName="Grade"
(selectionChange)="selectedGrade($event)"
multiple
required
>
<mat-option *ngFor="let s of gradeCodes" [value]="s.Code">
<span *ngIf="!s.Name">
</span>
<span *ngIf="s.Name">
{{s.Name}}
</span>
</mat-option>
</mat-select>
</mat-form-field>
selectedGrade(event: any) {
console.log('event', event);
let target = event.source.selected[0]._element.nativeElement;
let value = event.value[0];
let obj = {
code: value,
level: target.innerText.trim()
}
this.selectedLevel.push(obj);
console.log('grade', this.selectedLevel);
}
在这里,我的下拉菜单有多个选择选项。当用户从下拉列表中选择第一个值时,我需要相关的 value and text 将推入另一个数组,但根据我的情况,每个选择中都会出现相同的值。
【问题讨论】:
-
能否请您分享
gradeCodes对象给我一个样本? -
@errorau:
this.gradeCodes=[{Name:'Nurserry',Code:'A'},{Name:'Grade1',Code:'B'},{Name:'Grade2',Code:'C'}]。这是一个示例 fdata。