簡體   English   中英

在Angular材質中設置默認芯片

[英]Set default chip in Angular material

頁面加載時,我會選擇Web作為默認芯片。

     <mat-chip-list #chipsList >
        <mat-chip (click)="selectedChip(elm)" color="accent" selected="true" *ngFor="let elm of objectKeys" [value]="elm" color="accent">
          {{elm}}
        </mat-chip>
      </mat-chip-list>

實際上沒有選擇芯片。 我要選擇的是列表中的第一塊芯片。

當我console.log(this.chipsList)我看到所有選擇的項目都設置為true,但沒有人關注UI。

如何默認選擇芯片?

這是一個演示

嘗試將selected =“ Web”更改為[selected] =“ this.selected === elm”

嘗試在ngFor中使用索引

 <mat-chip (click)="selectedChip(elm)" color="accent" [selected]="index === 0" *ngFor="let elm of objectKeys; let index = index" [value]="elm" color="accent">
          {{elm}}</mat-chip>
      </mat-chip-list>

https://stackblitz.com/edit/angular-material-chip-default-select-vsexxr?file=app%2Fapp.component.html

甚至更好, 首先

[selected]="first" *ngFor="let elm of objectKeys; let first = first"

首先在打字稿中設置默認值:

tmpindex: number = 0;

現在像這樣在您的html中。

<mat-chip (click)="selectedChip(elm);tmpindex=i;" 
          color="accent" [selected]="i===tmpindex" 
          *ngFor="let elm of objectKeys; let i = index" 
          [value]="elm" color="accent">
     {{elm}}</mat-chip>
</mat-chip-list>

這是工作示例: 設置默認芯片列表

希望這可以幫助!!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM