【发布时间】:2017-10-26 02:36:42
【问题描述】:
我在 INTERNET 上进行了很多搜索,但仍然无法弄清楚如何在没有任何第三方的情况下制作自定义自动建议器。经过大量谷歌我发现this
但问题是我的 api 响应有点不同,我得到的响应是:
[{"id":"1","name":"aa"},{"id":"2","name":"bb"}...]
因此,我将 [object][object] 作为管道中的值。
谁能帮助我如何用这个管道处理这个请求。我希望他们应该是一个文本框,点击它应该有列表,并且在用户输入时,以下建议可能会有所不同。
管道:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'FilterPipe',
})
export class FilterPipe 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;
}
}
在ts中:
import { Component } from '@angular/core';
import {FilterPipe} from './pipes'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title:String;
names:any;
constructor(){
this.title = "Angular 2 simple search";
this.names = ['Prashobh','Abraham','Anil','Sam','Natasha','Marry','Zian','karan']
}
}
*** 这很好用,但在我的情况下 this.name 数组是不同的,如上所述。
【问题讨论】:
-
您想按名称属性过滤吗?如果是这样,只需将
el.toLowerCase()替换为el.name.toLowerCase()即可。 -
让我试试,顺便谢谢你的回复
-
是的,非常感谢。我需要做更多的改变,我会尝试。谢谢
标签: json angular typescript angular2-template angular2-forms