【发布时间】:2019-08-27 07:41:17
【问题描述】:
我计划使用下面描述的 Angular Material Multi-Select Template 来允许用户选择多个输入(并在一个名为 (click) 的函数中使用它们,因此不需要实时访问)。但是我不清楚如何访问用户选择的值。
我猜 'toppings' -FormControl()-Object 与它有关,但它不包含预期的可能选项数组中的字符串值。
编辑:下面更新了 Html 代码。
来源:https://material.angular.io/components/select/overview
//html
<mat-form-field>
<mat-label>Toppings</mat-label>
<mat-select [formControl]="toppings" multiple> //?
<mat-option *ngFor="let topping of toppingList" [value]="topping">{{topping}}</mat-option>
</mat-select>
</mat-form-field>
//extract from my equivalent
<mat-label>Genres</mat-label>
<mat-select [formControl]="genres" multiple>
<mat-option *ngFor="let availableGenre of availableGenres" [disabled] ="disableGenres" [value]="genre">{{availableGenre}}</mat-option>
</mat-select>
//ts
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
/** @title Select with multiple selection */
@Component({
selector: 'select-multiple-example',
templateUrl: 'select-multiple-example.html',
styleUrls: ['select-multiple-example.css'],
})
export class SelectMultipleExample {
toppings = new FormControl(); //?
toppingList: string[] = ['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage', 'Tomato'];
}
//extract from my equivalent in the related component.ts
availableGenres: string[]; //assigned in NgOnInit()
genres = new FormControl();
//update:
<mat-form-field>
<div>
<mat-label>Genres</mat-label>
<mat-select [(value)]="genres" multiple>
<mat-option *ngFor="let availableGenre of availableGenres" [disabled] ="disableGenres" [value]="genre">{{availableGenre}}</mat-option>
</mat-select>
</div>
</mat-form-field>
【问题讨论】:
-
this.toppings.value
-
@joyBlanks 不知何故,尽管选择的数量正确,但所有数组元素都未定义。知道是什么原因造成的吗?
标签: javascript angular angular7