2017-01-07 10 views
0

Angular formcontrolでfilterプロパティを使用しようとしています。rxjsフィルタで最小入力長を保証する方法

これはコードです:

import { Component, OnInit } from '@angular/core'; 
import { FormControl } from '@angular/forms'; 
import { SpotifyService } from '../services/spotify.service'; 
import { Artist } from '../models/models'; 
import { Observable, Subscription } from 'rxjs/Rx'; 

    @Component({ 
     selector: 'spt-search', 
     templateUrl: './search.component.html', 
     styles: [] 
    }) 
    export class SearchComponent implements OnInit { 
     public searchResult: Observable<Array<Artist>>; 
     public searchString: string; 
     public term = new FormControl; 
     constructor(private spotService: SpotifyService) { } 

     ngOnInit() { 
     this.searchResult = this.term.valueChanges 
      .debounceTime(400) 
      .distinctUntilChanged() 
      .filter((query: string) => query.length > 1) 
      .switchMap(term => this.spotService.searchMusic(this.term.value).map(res => res.artists.items)); 
     } 
    } 

私はこのエラーを取得するフィルタを設定しようとすると:

EXCEPTION: Uncaught (in promise): Error: Error in :0:0 caused by: this.term.valueChanges.debounceTime(...).distinctUntilChanged(...).filter is not a function 
TypeError: this.term.valueChanges.debounceTime(...).distinctUntilChanged(...).filter is not a function 
    at SearchComponent.ngOnInit (http://localhost:4200/main.bundle.js:229:14) 
    at Wrapper_SearchComponent.ngDoCheck (/AppModule/SearchComponent/wrapper.ngfactory.js:22:53) 
    at CompiledTemplate.proxyViewClass.View_SearchComponent_Host0.detectChangesInternal (/AppModule/SearchComponent/host.ngfactory.js:28:29) 
    at CompiledTemplate.proxyViewClass.AppView.detectChanges 
+0

こんにちは問題は長さですか? ..それは他の何かのように見える...または多分私は間違っています..あなたはどこかでフィルタ関数(ES6)を使用しようとしていますか? –

+0

私の悪い、私はフィルタなしで作業コードを表示していた。問題の原因となっているフィルタコードを追加しました。 –

答えて

0

こんにちは、私はあなたの問題は機能.distinctUntilChanged()は戻らないということだと思いますあなたは.filter()メソッドを添付することができます。しかし、これを試すことができます:

import { Component, OnInit } from '@angular/core'; 
import { FormControl } from '@angular/forms'; 
import { SpotifyService } from '../services/spotify.service'; 
import { Artist } from '../models/models'; 
import { Observable, Subscription } from 'rxjs/Rx'; 

    @Component({ 
     selector: 'spt-search', 
     templateUrl: './search.component.html', 
     styles: [] 
    }) 
    export class SearchComponent implements OnInit { 
     public searchResult: Observable<Array<Artist>>; 
     public searchString: string; 
     public term = new FormControl; 
     constructor(private spotService: SpotifyService) { } 

     ngOnInit() { 
     this.searchResult = this.term.valueChanges 
      .debounceTime(400) 
      .distinctUntilChanged() 
      .filter((query: string) => query && query.length > 1 ? query.length > 1 : "") 
      .switchMap(term => this.spotService.searchMusic(this.term.value).map(res => res.artists.items)); 
     } 
    } 

、または次のような伝播も可能です:

.filter((query: string) => query?.length > 1) 
+0

ありがとうございます。同じエラーが発生しました。 –

+0

.distinctUntilChanged()がARRAY(またはもっと良いコレクション)を返す場合は、doubleをチェックしてください。 –

+0

thnx Mark :-) .. –

関連する問題