是否可以在Angular Material多重选择中更改显示的值?我该怎么做,例如产品:(0/12)取决于所选项目的数量。
答案 0 :(得分:3)
为此使用<mat-select-trigger>
。例如:
<mat-form-field>
<mat-select placeholder="Toppings" [formControl]="toppings" multiple>
<mat-select-trigger>
{{toppings.value ? toppings.value[0] : ''}}
<span *ngIf="toppings.value?.length > 1" class="example-additional-selection">
(+{{toppings.value.length - 1}} {{toppings.value?.length === 2 ? 'other' : 'others'}})
</span>
</mat-select-trigger>
<mat-option *ngFor="let topping of toppingList" [value]="topping">{{topping}}</mat-option>
</mat-select>
</mat-form-field>
在此处查看演示:https://stackblitz.com/angular/qkjbojyxebly?file=app%2Fselect-custom-trigger-example.html
答案 1 :(得分:0)
除了上述答案之外,要将选择设置为初始值,这是在给定的stackblitz演示中扩展select-custom-trigger-example.ts
的方式:
import {Component, OnInit} from '@angular/core';
import {FormControl} from '@angular/forms';
/** @title Select with custom trigger text */
@Component({
selector: 'select-custom-trigger-example',
templateUrl: 'select-custom-trigger-example.html',
styleUrls: ['select-custom-trigger-example.css'],
})
export class SelectCustomTriggerExample implements OnInit {
toppings = new FormControl();
toppingList: string[] = ['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage', 'Tomato'];
ngOnInit()
{
// initalSelection could be the result from a service call (database, backend, etc.):
let initalSelection : string [] = ["Onion","Pepperoni","Sausage"];
// set initalSelection as inital value:
this.toppings.setValue(initalSelection);
}
}