如何获取Angular Material Multiselect的当前状态(Is Checked或Unchecked)

时间:2018-05-14 08:03:08

标签: angular material-ui angular-material2

我正在使用MatSelect和多个项目选择。我想要当前项目的状态为ex。选中或取消选中当前单击的项目。在国家的基础上,我将不得不打开一个对话框。

4 个答案:

答案 0 :(得分:2)

如果他们只有一些文件......

... Oh Wait !

  

selected: MatOption | MatOption[]当前选择的选项。

修改

在HTML

<mat-select #select (change)="x()" ...>
  <option *ngFor="let x of y" ...></option>
</mat-select>

在TS中

@ViewChild('select') select: MatSelect;

x() {
  console.log(this.select.selected);
}

现在您拥有所选的值(因此,未选择的值)

答案 1 :(得分:1)

在mat-option上,您可以添加单击事件并将其记录到跟踪:

<mat-option value="abc" (click)="onOptionClick($event)">abc</mat-option>

在.ts组件中:

onOptionClick(event){
console.log(event.target.selected);
}

答案 2 :(得分:1)

Easist方法只需将$ event.target.selected传递给.ts中的方法,您就可以根据状态打开一个对话框。

in .html

<mat-option value="val" (click)="onClick($event.target.selected)">val</mat-option>
<。>在.ts文件中

onOClick(isSelected){
   if(isSelected){
    //selected state
   }else{
   //not selected state
   }
}

答案 3 :(得分:1)

从Angular 6开始,这对我有用(仅在Safari中测试)。

HTML

<input (change)="yourFunction($event)" type="checkbox" name="your-name">

TS

private yourFunction(event)
{
   console.log(event.target.checked);
}