gpt4 book ai didi

angular - 通过单击按钮重置垫选择值

转载 作者:行者123 更新时间:2023-12-05 02:07:37 24 4
gpt4 key购买 nike

我有一个 mat-select 下拉菜单,用于在 UI 上过滤数据。我使用 getTeam 函数将数据从子组件发送到父组件

垫选择

<mat-form-field>
<mat-label>Select Team</mat-label>
<mat-select (selectionChange)="getTeam($event)">
<mat-option *ngFor="let team of teams" [value]="team.name">
{{team.name}}
</mat-option>
</mat-select>
</mat-form-field>
<span class="material-icons" (click)="reset()">delete_sweep</span>

过滤逻辑 (.ts)getTeamQuery 接收发出的值并过滤 UI 数据。

  videos: Video[] = videos;
filteredVideos: Video[] = videos;

getTeamQuery(queryEmitted: string) {
this.videos = this.filteredVideos.filter(video => {
return video.team === queryEmitted;
});
}


**Clearing the filter selection**

reset() {
this.videos = videos;
}

如何通过单击 Material 图标重置垫选择的选择并返回到初始状态?初始状态,表示mat-select值是占位符的状态。

最佳答案

只需在循环之前添加一个普通的 mat-option,如下所示:

<mat-select (selectionChange)="getTeam($event)">
<mat-option value="">Select a name...</mat-option>

<mat-option *ngFor="let team of teams" [value]="team.name">
{{team.name}}
</mat-option>

</mat-select>

编辑:根据您的评论,您可以在这种情况下使用 ngModel,如下所示:

html文件:

<mat-select (selectionChange)="getTeam($event)" [(ngModel)]="teamInitial">
// your code...
</mat-select>

ts 文件:

export class YourComponent {
teamInitial = '';

// you code...

reset() {
this.teamInitial = '';
}
}

关于angular - 通过单击按钮重置垫选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61596411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com