我一直在INTERNET上搜索很多,但仍然無法弄清楚如何在沒有任何第三方的情況下自定義自動提示器。經過大量的谷歌,我發現thisAngular2自動提示器
但問題是,從API我的反應是有點不同的我得到的反應是:
[{"id":"1","name":"aa"},{"id":"2","name":"bb"}...]
由於這我得到[對象] [對象]作爲管道中的值。
任何人都可以請幫助我如何處理這個請求與這個管道。我希望他們應該是一個文本框,在其上應該列出的點擊和用戶輸入上,下面的建議可能會有所不同。
管:
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.name.toLowerCase()'替換'el.toLowerCase()',它就可以工作。 – developer033
讓我試試,順便謝謝回覆 –
是的,它表示非常感謝。我需要做一些更改,我會嘗試。謝謝 –