【问题标题】:Angular search after click event点击事件后的角度搜索
【发布时间】:2019-11-28 11:07:24
【问题描述】:

我最近在使用 Angular,我需要帮助。

我有一张带有 Angular Material 的表格,在这张表格中我有很多项目,我有一个像这样的“搜索框”:

<mat-form-field>
  <input matInput [(ngModel)]="SearchText" placeholder="Filtra i risultati" style="color: black;">                     
</mat-form-field>

<button mat-button (click)="applyFilter($event.target.value)" class="search-button">
  <mat-icon>search</mat-icon>
</button>

TS

applyFilter(filterValue: string) {
    filterValue = filterValue.trim();
    filterValue = filterValue.toLowerCase();
    this.dataSource.filter = filterValue;
}

实际上我想要达到的结果是,项目列表必须只有在框中写入内容并单击按钮后才会出现。

所以表格最初是空的。

我写了一些东西,然后单击搜索按钮,就会出现请求的结果。

我怎样才能得到这个结果? 你能帮我处理 HTML 和 TS 吗?

谢谢

【问题讨论】:

  • 检查控制台是否有错误

标签: angular button search visual-studio-code angular-material


【解决方案1】:

(click)="applyFilter($event.target.value)" 更改为(click)="applyFilter(SearchText)"

$event.target.value 表示同一个输入元素的值,这里需要按钮元素的输入值。所以使用模型值SearchText

试试这样:

<mat-form-field>
   <input matInput [(ngModel)]="SearchText" placeholder="Filtra i risultati" style="color: black;">                     
</mat-form-field>

<button mat-button (click)="applyFilter(SearchText)" class="search-button">
  <mat-icon>search</mat-icon>
</button

【讨论】:

  • 我需要更改我的 TS 吗?因为当我点击按钮搜索时它仍然不起作用。
  • console.log(filterValue ) 在您的 .ts 中。如果它与您输入的内容相同,那么您就可以开始了。无需更改任何内容,前提是您的后端工作正常。请记住,您已经再次调用 API。仅设置数据源过滤器值不起作用
  • 是的,使用 console.log(filterValue) 我有相同的搜索结果,但表中没有任何反应。不过滤。 :(
  • 检查你的后端。
  • 我在控制台中出现这个错误:Unexpected token 在 JSON 中的位置 0 处查找某些东西。其中“查找某些东西”是我搜索的词。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
相关资源
最近更新 更多