angular - angular6 Material 垫选择列表中的当前选定值

标签 angular angular-material angular-material2 angular6

使用 Angular Material2 ma​​t-selection-list,能够识别当前选项是被选中还是未被选中[Boolean]。

compnenent.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>

component.ts

export class ListSelectionExample {
  typesOfShoes = ['Boots', 'Clogs', 'Loafers', 'Moccasins', 'Sneakers'];

  onSelection(e, v){
   console.error(e.option.selected,v); 
  }
}

e.option.selected 通知当前选项是选中还是未选中。

如何识别当前选择的值?尝试与 ngModel 和 ngModelChangeclick 进行多种组合,但对我没有任何作用。

https://stackblitz.com/edit/angular-eyjdfp-qgjhvd?file=app%2Flist-selection-example.ts

最佳答案

您可以通过获取 selectionChange $evente.option.value 来获取当前选择的值

component.ts

current_selected: string;

onSelection(e, v){
   this.current_selected = e.option.value;
}

component.html

<p>
  Current selected value: {{ current_selected }}
</p>

奖金

您可以通过在模板中调用shoes.selectedOptions.selected 的属性selected 来列出所有选中的项目。

component.html

<p>Selected:</p>
<ul>
  <li *ngFor="let i of shoes.selectedOptions.selected">
    {{ i.value }}
  </li>
</ul>

Working Demo

关于angular - angular6 Material 垫选择列表中的当前选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50636365/

相关文章:

javascript - 有关 Angular Material $mdDialog.finally 的文档?

带有 Firestore 的 Angular Material MatTableDataSource

angular - 如何在 Electron 中创建一个截取整个桌面屏幕截图的后台进程?

angular - 模块错误(来自./node_modules/postcss-loader/src/index.js): (Emitted value instead of an instance of Error) CssSyntaxError

javascript - 在 Angular 5 中重新渲染(重新加载)路由器导出

angular - MatSidenav 和 MatDrawer 有什么区别?

angular - 奇怪的路由问题 Angular 7 + Ionic 4 Beta

angular-material - 找不到树的树控件

angular - 自定义 Angular Material 2 工具提示样式

angular-material2 - 使用 flex 布局在 angular 2 Material 中开发响应式布局