【发布时间】:2020-07-06 09:44:16
【问题描述】:
我正在做 Angular 项目,我想让用户从现有标签(mat-chips)中搜索/过滤。我有一个搜索框,我可以过滤一个普通列表,但是当我尝试对标签执行此操作时,我不确定如何。
我的垫子在 home.component.html 中。
<mc-tags [chips] = "tags" ></mc-tags>
我在 home.component.html 中的搜索框
<input matInput (input)="updateQuery($event.target.value)" class="input" >
list.ts 中的数据
export const tags = ['Google', 'Manufacturer'];
home.component.ts 文件
import { Component, OnInit } from '@angular/core';
import { users, tags } from './users.data';
@Component({
selector: 'mc-explore',
templateUrl: './explore.component.html',
styleUrls: ['./explore.component.scss']
})
export class ExploreComponent{
query: string;
users = users;
tags = tags;
updateQuery(query: string) {
this.query = query;
}
}
这就是现在的样子
这就是我通常过滤普通列表/数据的方式
<div [hidden]="!query">
<div *ngFor="let name of users | search:query">{{ name }}</div>
</div>
没有 mc-tags 的 Stackblitz 文件,因为它使用不同的组件
【问题讨论】:
-
你能创建一个 stackBlitz 来检查它吗?
-
@Tzimpo 我刚刚创建了它(stackblitz.com/edit/angular-vcklft),但我没有包含
因为它是从不同的组件中使用的。交易 -
我不确定我是否明白了……您希望用户单击一个芯片并使用所选芯片的值过滤用户数组,对吗?
-
@julianobrasil umm 现在,我只希望用户在搜索框中搜索芯片并仅显示匹配的芯片并隐藏其余芯片。 (不介意的话可以看我的附图)
-
好吧...您只想开始输入芯片(标签)的名称并过滤它们。
标签: javascript angular search filter tags