angular - 在 ts 文件中显示从 mat-select 多个中选择的值
问题描述
我正在使用多个。我想在控制台中显示选定的值。我正在使用 Angular 6 Material design 我在 html 中获得了价值。但我想在 ts 文件中以逗号分隔所有选定的值。下面是我的代码。
<form [formGroup]="searchUserForm" fxFlex fxLayout="column" autocomplete="off" style="margin: 30px">
<mat-select placeholder="User Type" formControlName="userType" multiple>
<mat-option *ngFor="let filters of userTypeFilters" [value]="filters.key" (click)="tosslePerOne(allSelected.viewValue)">
{{filters.value}}
</mat-option>
<mat-option #allSelected (click)="toggleAllSelection()" [value]="0">All</mat-option>
</mat-select>
</form>
import { Component, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, FormControl } from '@angular/forms';
import { MatOption } from '@angular/material';
@Component({
selector: 'material-app',
templateUrl: 'app.component.html'
})
export class AppComponent {
searchUserForm: FormGroup;
userTypeFilters = [
{
key: 1, value: 'Value 1',
},
{
key: 2, value: 'Value 2',
},
{
key: 3, value: 'Value 3',
},
{
key: 4, value: 'Value 4',
}
];
@ViewChild('allSelected') private allSelected;
constructor(private fb: FormBuilder){}
ngOnInit() {
this.searchUserForm = this.fb.group({
userType: new FormControl('')
});
}
tosslePerOne(all){
if (this.allSelected.selected) {
this.allSelected.deselect();
return false;
}
if(this.searchUserForm.controls.userType.value.length==this.userTypeFilters.length)
this.allSelected.select();
}
toggleAllSelection() {
if (this.allSelected.selected) {
this.searchUserForm.controls.userType
.patchValue([...this.userTypeFilters.map(item => item.key), 0]);
} else {
this.searchUserForm.controls.userType.patchValue([]);
}
}
}
谁能帮助我如何做到这一点。
解决方案
您应该能够multiple
通过this.searchUserForm.get('userType').value
.
根据您在 中绑定的内容,value
您mat-option
将获得一组不同的值/对象。
要获得包括key
and在内的完整对象,请value
像这样绑定它[value]="filters"
。如果您只需要其中一个属性的数组,请将该属性添加到绑定中。
推荐阅读
- javascript - Typescript/Node 文件自动检测来自其他文件的未导出变量
- claims-based-identity - 使用 Microsoft Identity 从 Web Api 中的扩展 UserClaim 表中获取声明
- bash - Bash 三元运算符可以用来给多个变量赋值吗?
- node.js - 在 docker 中运行的 Node.js WebsocketServer (wss/ws) 显示与代码 1006 的连接断开(随机)
- r - 抑制图例中的元素
- linux - /proc/interrupts - 在 c 中读取显示没有 IPI
- python - 为独立条目填充 NumPy 数组的有效方法?
- excel - Excel VBA - 每个唯一组的 N 个随机记录
- postgresql - 如何编写 PostgreSQL 存储过程来查找和终止空闲查询?
- javascript - 使用 chart.js 的实时滚动图表