【发布时间】:2019-01-01 16:14:27
【问题描述】:
我正在我的应用程序中创建一个简单的过滤器管道,这就是我所拥有的。
过滤组件:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'dataFilter'
})
export class DataFilterPipe implements PipeTransform {
transform(value: any[], input: string) {
if (input) {
input = input.toLowerCase();
return value.filter(function (el: any[]) {
return el.toLowerCase().indexOf(input) > -1;
});
}
return value;
}
}
这里是 componet.html
<div class="card movies-list" id="movies-list">
<div class="card-header movies-list_header">
<h1>Content: <small>Best movies of 2010's</small></h1>
</div>
<div class="card-body">
<div class="row movies-list_filter">
<div class="col-md-2">Filter By</div>
<div class="col-md-4">
<input type='text' [(ngModel)]="listFilter">
</div>
</div>
<div class="row">
<div class="col-md-6">
<h1>filterdbY: {{listFilter}}</h1>
</div>
</div>
<table class="table table-striped table-responsive">
<thead>
<tr class="movies-list_tbheader">
<td></td>
<th>Title</th>
<th>Realease</th>
<th>Rating</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="movies-list_data" *ngFor="let movie of movies | dataFilter:listFilter">
<td>
<img *ngIf="movie.poster_path" class="thumbnail" src="http://image.tmdb.org/t/p/w500/{{movie.poster_path}}">
</td>
<td>{{ movie.title }}</td>
<td>{{ movie.release_date }}</td>
<td>{{movie.vote_average}}</td>
<td>{{movie.overview}}</td>
</tr>
</tbody>
</table>
</div>
</div>
在 component.ts 中我声明了变量 listFilter 如下:
export class MoviesComponent implements OnInit {
listFilter = '';
-----------
}
当我运行我的应用程序并尝试搜索一些文本时,我收到以下错误
ERROR TypeError: el.toLowerCase 不是函数
我的代码有什么问题?任何帮助将不胜感激
【问题讨论】:
标签: javascript angular html typescript bootstrap-4