简体   繁体   English

在垫状滑动切换按钮处于打开状态时为其添加Angular 7芯片

[英]Adding Angular 7 chips for mat-slide-toggle buttons when they are On state

On toggle of starter chip should be added to simocode li 在启动器芯片切换时,应将其添加到simocode li

<mat-tab label="Device" >
        <ul>
            <mat-slide-toggle (change)="onChange($event)" >
            <!--li for chips-->
            <li>Simocode</li>
            </mat- `enter code here`
            slide-toggle>
            <mat-slide-toggle>
                <li>starter</li>
            </mat-slide-toggle>
        </ul>
    </mat-tab>

The case is on toggle active you need to add the chip with text 'Starter' and on toggle inactive you need to remove the chip. 这种情况是在切换active您需要添加带有文本“ Starter”的芯片,而在切换inactive您需要移除芯片。

Here is the code for template 这是模板的代码

  <mat-slide-toggle (change)="onToggle($event)"> Starter </mat-slide-toggle>
  <mat-chip-list>
    <mat-chip *ngFor="let chip of chips">{{chip}}</mat-chip>
  </mat-chip-list>

And in your component. 并在您的组件中。

export class AppComponent {
  chips: any = [];

  onToggle(event) {
    let { checked } = event;
    const text = event.source._elementRef.nativeElement.innerText;
    if(checked) {
      this.chips.push(text);
    } else {
      let index = this.chips.indexOf(text);
      this.chips = this.chips.slice(1, index);
    }
  }
}

Here's the stackblitz example 这是stackblitz的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM