1

Ng-Bootstrapのtypeahead directiveを使用するAngular 4アプリケーションを開発しています。 Wikipediaの例では、Wikipediaへのapi呼び出しを行い、結果をtypeaheadボックスに表示します。私は同じことをやろうとしていますが、代わりにGoogle Mapの場所自動補完サービスを使っています。GoogleマップでNg-Bootstrap Typeaheadのスタイルが正しくありませんPlace Autocomplete

ウィキペディアの例に続いて、私はオートコンプリートされた場所を含むRxjs Observableを返す同様のサービスを作成しました。コントローラ側で

search(term: string) { 
    if (term === '') { 
     return Observable.of([]); 
    } 

    return Observable.create(observer => { 
     this.autocompleteService.getPlacePredictions({ input: term }, (results, status) => { 
      if (status == google.maps.places.PlacesServiceStatus.OK) { 
      observer.next(results.map(result => result.description)); 
      observer.complete(); 
      } else { 
      console.log('Error - ', results, ' & Status - ', status); 
      observer.next({}); 
      observer.complete(); 
      } 
     }); 
    }); 
} 

、私のコードは次のようになります。

search = (text$: Observable<string>) => 
    text$ 
     .debounceTime(300) 
     .distinctUntilChanged() 
     .switchMap(term => 
      this.service.search(term) 
       .do(() => this.searchFailed = false) 
       .catch(() => { 
        this.searchFailed = true; 
        return Observable.of([]); 
       })) 

これはうまく動作しますが、何かが再描画をトリガするまで、いくつかの理由で、先行入力バーが誤った場所に表示されます。

Note the gap between the input field and the typeahead on the top image

いずれかのキーを押すか、画面上の任意の場所をクリックすると、即座にそれを修正し、私はそれが最初の時間を修正取得する方法を見つけ出すことはできません。

NgZoneの外で実行されているGoogleマップの場所オートコンプリートサービスで問題が発生する可能性があるようですが、再描画をトリガーしませんが、手作業で再描画(ApplicationRef.tick()、NgZone.run()、ChangeDetectorRef.detectChanges())。

ご意見をいただければ幸いです!

編集:類似の挙動とPlunker: https://embed.plnkr.co/iy2Zhd5rEhBK2aVbBqsB/

+0

通常、NgZone.run()はこのトリックを行う必要があります。私たちが見えるように問題を再現するプランナーをまとめることができますか? –

+0

NgZone.run()の実行をお勧めしますか? –

+0

外部API(Googleマップ)への呼び出しが行われる場所です。もう一度、再現シナリオを持つプランクを持つと、私たちはもっと速く答えを得ることができます:-) –

答えて

0

私はそれを得ました! NgZoneはこのトリックをやってしまったが、に入れてラップするのではなく、コールバックにしなければならなかった。

search(term: string) { 
    if (term === '') { 
    return Observable.of([]); 
    } 

    let result = Observable.create(observer => { 
    this.autocompleteService.getPlacePredictions({ input: term }, (results, status) => { 
     this.ngZone.run(() => { 
     if (status == google.maps.places.PlacesServiceStatus.OK) { 
      observer.next(results.map(result => result.description)); 
      observer.complete(); 
     } else { 
      console.log('Error - ', results, ' & Status - ', status); 
      observer.next({}); 
      observer.complete(); 
     } 
     }); 
    }); 
    }); 
    return result; 
} 
関連する問題