2016-09-15 105 views
-1

如何寫一些@Pipe用於過濾(在<input>標籤)在表中的數據?濾波器輸入在角2

<tr *ngFor='let list of lists'> 
     <td><input type="" name="" value=""></td> 
     <td>{{ list.name }}</td> 
     <td>{{ list.location }}</td> 
     <td>{{ list.type_id }}</td> 
    <tr> 

的數據,我從API獲得與HTTP服務:

getServices(): Observable<any> { 
     return this._http.get(this._url) 
      .map(res => res.json()); 
    } 

UPD:

這是我的服務組件:

import {Injectable} from '@angular/core'; 
import {Http, Headers, RequestOptions} from '@angular/http'; 
import {Observable} from 'rxjs/Observable'; 

import 'rxjs/add/operator/map' 

@Injectable() 
export class TableComponentService{ 
    private _url = 'http://101.496.222.511:8080/api/v1/10'; 

    constructor(private _http: Http) { 

    } 

    names:string[]; 
    getServices(): Observable<any> { 
     return this._http.get(this._url) 
      .map(res => res.json()); 
    } 

} 

,這是一個表組件

import { Component, OnInit } from '@angular/core'; 
import { TableComponentService } from './table.service'; 

@Component({ 
    selector: 'tablecomponent', 
    templateUrl: 'app/table.template.html', 
    providers: [TableComponentService] 
}) 
export class TableComponent implements OnInit { 
    lists: any[]; 

    constructor(private _service: TableComponentService) { 

    } 

    ngOnInit() { 
     this._service.getServices() 
      .subscribe(lists => this.lists = lists) 
    } 

} 
+0

u能解釋一下你在''標籤我寫的一些符號filering正是 –

+0

@Atal紀的意思,然後找到完整的單詞。例如,寫一個 - 然後找到阿爾法 –

+0

爲什麼ü想使用管道而不是在改變事件只是調用getservices(),並存儲在一個變量接收到的值,並利用可變 –

回答

1

相反管的使用keyup事件通過使用可變

<tr *ngFor='let list of lists'> 
     <td><input type="" name="" value="" (keyup)="getServices()" > 
     <span *ngFor='let name of names'>{{name }}</span> 
     </td> 
     <td>{{ list.name }}</td> 
     <td>{{ list.location }}</td> 
     <td>{{ list.type_id }}</td> 
<tr> 


names:string[]; 
getServices(): Observable<any> { 
    this.names=this._http.get(this._url) 
     .map(res => res.json()); 
} 

EDIT調用getservices()和存儲在一個變量接收到的值,並顯示結果,無論你想: -

使_Service公衆構造

constructor(public_service: TableComponentService)

然後在HTML

<input type="" name="" value="" (keyup)="_service.getServices()" >

+0

感謝快速回答,現在,我有這個錯誤'core.umd.js:5995 EXCEPTION:錯誤在app/table.template.html:31:69造成的:self.context.getServices不是function' –

+0

化妝功能如公共 –

+0

'公共getServices():可觀察 { 返回this._http.get(this._url) .MAP(RES => res.json()); }'這樣嗎? –