2017-10-17 8 views
1

サードパーティ製のAPIからデータを取得するためにストリームを購読していますが、その間に回転するホイールでdivを表示します。データを取得すると、そのデータがそのdivに置き換えられます。ngifにdivを重複するAngular4

<div *ngIf="my-data;else loading" class="my-data-container"> 
    ... 
</div> 
<ng-template #loading> 
    <div class="loading-container"> 
     shows the loading spinning wheel  
    </div> 
</ng-template> 

私が持っている問題は、時間の本当に短い期間で、両方のdivが画面に表示され、彼らは小さなブリップのような不思議な効果を作ることです:私はngif elseていることがあります。私は彼らが互いに重なり合うようにしたい。コードでそれを達成することはできますか、それともCSSのトリックを調べる必要がありますか?

+0

あなたはどのように初期化します私のデータ? – Vega

+0

これは、ngOnInitメソッドで観測値のサブスクリプションを介して行います。 ngOnInit(){ this.myDataService.get() .subscribe(g => {this.my-data = g}); } –

+0

divコンテンツを他のng-templateに移動し、ngIfにoptionを追加します。 – Vega

答えて

1

あなたは開始時にnullとしてmy-data値を宣言し作成しthenを使用するようにngIf構文を変更します。

<div *ngIf="my-data; then #content; else loading" class="my-data-container"> 
</div> 
<ng-template #content> 
    show this content after loading 
</ng-template 
<ng-template #loading> 
    <div class="loading-container"> 
     shows the loading spinning wheel  
    </div> 
</ng-template> 
1

代わりのdivそれを自己のテンプレートngifとして使用コンテナ

<ng-container *ngIf="my-data;else loading"> 
    <div class="my-data-container"> 
    ... 
    </div> 
</ng-container> 
<ng-template #loading> 
    <div class="loading-container"> 
     shows the loading spinning wheel  
    </div> 
</ng-template> 
関連する問題