angular - Angular(和 JHipster)的问题:value.toLowerCase 不是函数
问题描述
使用我的过滤器(使用字段搜索过滤实体的行),会出现此问题:ERROR TypeError: "value.toLowerCase is not a function"
。我不知道为什么以及如何解决它。searchText = searchText.toLowerCase();
我的实体的字段是除 ID 外的字符串,当我在 .ts 中评论该行或删除toLowerCase()
时问题仍然存在return it.toLowerCase().includes(searchText);
更新:即使出现问题:
export class FilterPipe implements PipeTransform {
transform(items: any[], searchText: string): void {
}
所以我真的想知道是否没有 JHipster 限制,因为它只适用于 Angular as ng serve
。
.ts :
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
transform(items: any[], searchText: string): any[] {
if (!items) {
return [];
}
if (!searchText) {
return items;
}
searchText = searchText.toLowerCase();
return items.filter(it => {
return it.toLowerCase().includes(searchText);
});
}
}
.html:
<div class="table-responsive" *ngIf="laboratories">
<table class="table table-striped">
<thead>
<tr>
<th><span>ID</span></th>
<th><span>Name</span></th>
<th><span>Adress</span></th>
<th></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let laboratory of laboratories | filter:searchText ;trackBy: trackId">
解决方案
您正在对collections进行迭代。当你使用 ngFor 时,它会遍历一个数组。我认为这是一系列实验室。
这意味着您的对象是实验室。
在您的代码中,您认为您的实验室是一个字符串:
return items.filter(it => {
return it.toLowerCase().includes(searchText);
});
也许你应该添加一些字段来过滤?
return items.filter(it => {
return it.name.toLowerCase().includes(searchText);
});
另外,正如我在评论中所说:
推荐阅读
- package - 创建调用包函数的视图 (MariaDB 10.6.4)
- javascript - 在Javascript中四舍五入整数
- javascript - 带有条件的过滤选项 JS / VUE
- oracle - 在 PL/SQL 中具有动态绑定的动态 SQL
- php - 无法在 Apple M1 Pro 上安装 phalcon php
- laravel - 数据未保存在数据库中
- codeigniter - 在理论上 [in] 正确的跨子项目共享的 codeigniter 模型是否正确?
- android - 系统:忽略标头 X-Firebase-Locale,因为它的值为 null
- php - PHP 使用 htmlspecialchars 格式化 exec 命令的用户输入
- python - 同步所有文件夹outlook win32 Python