2017-04-05 60 views
1

我有一個角度爲2的應用程序,我已經設法在我的頁面上設置一個輸入,其自動完成功能可以調用api並確保服務器端過濾和返回值。很像這裏找到的教程。Angular 2加載數據一次,並在本地過濾Observable

現在,我正在嘗試給我的頁面多輸入一些內容,但是我不需要在服務器端對它們進行過濾。那將是無效的。更好的是隻要我的組件加載,然後過濾組件中的所有值。這導致我不缺問題。我有一個API調用,返回我需要的3個字符串數組。我正在使用的標準方法,像這樣來自一個角服務:

getFormFilters(): Observable<FormFilterModel> { 
    return this._http.get(this.baseUrl+this.getFormFiltersPath) 
     .map(res => res.json() as FormFilterModel); 
    } 

型號:

export interface FormFilterModel { 
    array1: string[]; 
    array2: string[]; 
    array3: string[]; 
} 

這工作得很好,我也得到我的觀察到後部。我被卡住的地方是如何現在過濾這3個數組?我有我的輸入連接到我的表單控件,就像我對服務器端過濾輸入。我無法弄清楚如何到達我的Observable中的實際數組來過濾它們。下面是我在哪裏與代碼:

this.filteredArray1$ = this.searchForm.controls.array1Search.valueChanges 
     .debounceTime(300) 
     .distinctUntilChanged() 
     .switchMap(term => //some filtering here of formFilterModel$ to return a subset of array1 based on input) 

我可以過濾通過正則表達式就好了一個數組,但得到在觀察到實際的數組是我似乎無法做到。

+0

您可以使用Let。看看這篇文章本週出來! https://netbasal.com/take-advantage-of-the-let-operator-in-angular-d351fd4bd1d9 – Dan

回答

0

訂閱你的API調用存儲結果的組件。然後,過濾valuesChanges.map中的陣列。

ngOnInit() { 
    this.getFormFilters().subscribe(formFilters => { 
    this.formFilters = formFilters; 
    }); 

    this.filteredArray1$ = this.searchForm.controls.array1Search.valueChanges 
    .debounceTime(300) 
    .distinctUntilChanged() 
    .map(search => this.formFilters.array1.filter(value => matches(value, search))) 
} 
0

首先,您必須訂閱響應,然後過濾收到的對象。

formfilterData :FormfilterModel; 
filteredArray1:stringp[]=[]; 
this.serviceOb.getFormFilters().subscribe((formfilterData)=>{ 
    this.formfilterData=formfilterData; 


    }); 
    this.formfilterData.array1.filter((data)=>{ 

    this.formfilterData.push(data); 
    })