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

问题描述 投票:1回答:4

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

angular material-ui angular-material2
4个回答
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
投票

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

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

在.ts组件中:

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

1
投票

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

在.html

<mat-option value="val" (click)="onClick($event.target.selected)">val</mat-option>

在.ts文件中

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

1
投票

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

HTML

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

TS

private yourFunction(event)
{
   console.log(event.target.checked);
}
© www.soinside.com 2019 - 2024. All rights reserved.