2016-10-21 2 views
0

検索入力ボックスがあるホームページがあり、結果を表示する別のページにルーティングします。その結果のページには、返された観測可能性(searchResult)をループする* ngForがあります。Observableが空になったときに便利な情報を表示したい

私は<div *ngIf="searchResult">を持っています。これは、観測値が空であるかどうかを示しています。ただし、ホームページから結果ページにコンポーネントが初期化されると、このdivはすぐに読み込まれ、検索結果が返された後に消えます。 Observableが空であるかどうかを調べるもう1つの方法はありますか?このため、* ngIf = "searchResult"を使用する代わりにブール値を使うことができます。ここ

getDocument(searchText:string) { 
     this._contractService.getDocument(searchText) 
      .subscribe(
      document => this.document = document, 
      error => this.errorMessage = <any>error); 

    } 

私* ngIfです:

<div id="search-not-found" class="col-xs-12" *ngIf="!searchResult"> 
<div class="alert alert-success" role="alert"><p>Sorry, we did not find any match related to your search"</p></div> 
     </div> 

私は感謝したい任意のヘルプは

は、ここに私のサブスクリプションです。ありがとう

答えて

0

私は問題を解決できました。ブール変数(searchResultEmpty:boolean=false)を追加し、それをfalseとして初期化しました。それから私のサブスクリプションで私は真にブール値を変更します。ここ

は私の更新サブスクリプションです:

<div *ngIf="searchResultEmpty"> 
<div id="search-not-found" class="col-xs-12" *ngIf="!searchResult"> 
<div class="alert alert-success" role="alert"><p>Sorry, we did not find any match related to your search"</p></div> 
     </div> 
</div> 
:ここ

getDocument(searchText:string) { 
     this._contractService.getDocument(searchText) 
      .subscribe(
      document => {this.document = document, this.searchResultEmpty=true}, 
      error => this.errorMessage = <any>error); 

    } 

はdiv要素であります

関連する問題