angular - angular6 材质 mat-selection-list 中的当前选定值
问题描述
使用 Angular Material2 mat-selection-list,能够识别当前选项是选中还是未选中[Boolean]。
组件.html
<mat-selection-list #shoes (selectionChange)="onSelection($event, shoes.selectedOptions)" >
<mat-list-option *ngFor="let shoe of typesOfShoes" [value]='shoe'>
{{shoe}}
</mat-list-option>
</mat-selection-list>
组件.ts
export class ListSelectionExample {
typesOfShoes = ['Boots', 'Clogs', 'Loafers', 'Moccasins', 'Sneakers'];
onSelection(e, v){
console.error(e.option.selected,v);
}
}
e.option.selected
通知当前选项是选中还是未选中。
如何识别当前选择的值?尝试了ngModel 和 ngModelChange的多种组合并单击,对我没有任何作用。
https://stackblitz.com/edit/angular-eyjdfp-qgjhvd?file=app%2Flist-selection-example.ts
解决方案
You can get current selected value by getting e.option.value
of your selectionChange $event
component.ts
current_selected: string;
onSelection(e, v){
this.current_selected = e.option.value;
}
component.html
<p>
Current selected value: {{ current_selected }}
</p>
Bonus
You can list all selected items by calling property selected of shoes.selectedOptions.selected
in the template.
component.html
<p>Selected:</p>
<ul>
<li *ngFor="let i of shoes.selectedOptions.selected">
{{ i.value }}
</li>
</ul>
推荐阅读
- ios - 将 Flutter App 的 iOS 部分连接到 Firebase 的更多问题
- java - 忽略 AndroidX 并想在 Android Studio 3.2 中单独使用 android 支持库
- docker - 有没有办法在非根 docker 容器中创建可写卷?
- android - 蓝牙连接安卓
- c - 有人可以解释我的代码做什么/如何修复它(bst 中的搜索节点)吗?
- javascript - 如何将图像发送到jquery中的视图
- linux - 选择值时显示额外列的 Vala 组合框
- c++ - 我还可以添加什么来通过空格或空格正确拆分 C++ 中的字符串?
- excel - 如何自动更改颜色?
- ios - 如何在 iOS 的 Web Audio 实现中平移音频?