2016-08-19 13 views
2

ng2-bootstrap TypeaheadをRESTバックエンドで使用しようとしています。matches.sliceは関数ではありませんng2-bootstrap先読み

HTTPレスポンス

[{ 
    "productDescription": "MAIL MKTG NIKLES HIGHLIGHT TUBE", 
    "productNumber": "10667" 
}, { 
    "productDescription": "SIGHT GLASSES/VAC BREAKER BR 15MM BSP", 
    "productNumber": "100436" 
}, { 
    "productDescription": "SIGHT GLASSES/VAC BREAKER BR 15MM BSP", 
    "productNumber": "100438" 
}] 

app.component.ts

import {TYPEAHEAD_DIRECTIVES} from 'ng2-bootstrap/ng2-bootstrap'; 

@Component({ 
    selector: 'my-app', 
    directives: [TYPEAHEAD_DIRECTIVES, CORE_DIRECTIVES, FORM_DIRECTIVES, REACTIVE_FORM_DIRECTIVES], 
    templateUrl: './app/typeahead-demo.html', 
    providers: [HTTP_PROVIDERS, FreightService] 
}) 
export class TypeaheadDemoComponent { 
    public stateCtrl:FormControl = new FormControl(); 
    public myForm:FormGroup= new FormGroup({ 
    state: this.stateCtrl 
    }); 

    public selected:string = ''; 
    public dataSource:Observable<any>; 
    public asyncSelected:string = ''; 
    public typeaheadLoading:boolean = false; 
    public typeaheadNoResults:boolean = false; 

    public constructor(private freightService: FreightService) { 
    this.dataSource = Observable.create((observer:any) => { 
     observer.next(this.asyncSelected); 
    }).mergeMap(() => this.getStatesAsObservable()); 
    } 

    public getStatesAsObservable():Observable<any> { 

    return Observable.of(
     this.freightService 
      .getMatchingProduct(this.asyncSelected).map(error => console.log(error)) 
    ); 
    } 
} 

freight.service.ts

私は偽のREST応答にmocky.ioを使用しています。

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

@Injectable() 
export class FreightService { 

    constructor(private http: Http) { 

    } 

    getMatchingProduct(productKeyword: string): Observable <string> { 

     return this.http.get("http://www.mocky.io/v2/57b6988e0f0000b8020b7996") 
      .map(this.extractData); 
    } 

    private extractData(res: Response) { 
     let body = res.json(); 
     return body.data || { }; 
    } 
} 

先行入力-demo.html

<h4>Asynchronous results</h4> 
    <pre class="card card-block card-header">Model: {{myForm.value.state | json}}</pre> 
    <form [formGroup]="myForm"> 
    <input formControlName="state" 
     [(ngModel)]="asyncSelected" 
     [typeahead]="dataSource" 
     (typeaheadLoading)="changeTypeaheadLoading($event)" 
     (typeaheadNoResults)="changeTypeaheadNoResults($event)" 
     (typeaheadOnSelect)="typeaheadOnSelect($event)" 
     [typeaheadOptionsLimit]="7" 
     [typeaheadOptionField]="'productDescription'" 
     placeholder="Locations loaded with timeout" 
     class="form-control"> 
    </form> 

私は本当にエラーで立ち往生しています "例外TypeError:matches.sliceは関数ではありません" あなたはすべての結果をマッピングしているように見えます

答えて

1

この行では定義されていません:.map(error => console.log(error))。それはエラーをキャッチしません、それは単に定義されていないconsole.log()の結果にすべての値をマップします。

また、getMatchingProductの結果はObservableであり、Observable.ofの必要はありません。

エラーを処理する場合は、.catchメソッドを試すことができます。

+0

エイドリアン、ご返信いただきありがとうございます。あなたが言ったように、私は以下のようにgetStatesAsObservableを変更しましたが、同じエラーが発生しました。助けてください!!公共getStatesAsObservable():観測可能 { 戻りthis.freightService .getMatchingProduct(this.asyncSelected) の.map((製品)=> this.products =製品) }あなたはまったく同じことをやっている –

+0

が、中に別の方法。結果を何かにマップする必要はありません(確かに未定義ではありません)、ストリームは結果を保持します。 'this.products = product'の結果は未定義となるため、同じエラーが発生します。演算子をよりよく理解するには、[RxMarbles](http://rxmarbles.com/#map)をご覧ください。 –

+0

また、反応性プログラミングについて少しお読みください。 「何か不足しているリアクティブ・プログラミングの紹介」(https://gist.github.com/staltz/868e7e9bc2a7b8c1f754)のようなものが役立ちます。 –

1

以下は機能しました!

app.component.ts

public constructor(private freightService: FreightService) { 
    this.dataSource = Observable.create((observer:any) => { 
     this.freightService.getMatchingProduct(this.asyncSelected) 
     .subscribe((result: any) => { 
     observer.next(result); 
     }) 
    }); 
    } 

freight.service.ts

@Injectable() 
export class FreightService { 

    constructor(private http: Http) { 

    } 

    getMatchingProduct(productKeyword: string): Observable <any> { 

     return this.http.get("app/test").map(this.extractData); 
    } 

    private extractData(res: Response) { 
     let body = res.json(); 
     return body || { }; 
    } 
} 
関連する問題