2017-10-09 11 views

답변

1

할 수있는 방법이 몇 가지 있습니다. 나는 두 가지 방법을 언급합니다 :

는 말할 수 있습니다 prop 속성의 값은 일부 외부 소스 (예 : HTTP 호출)에 따라 다음 템플릿에 당신이 할 수있는 다음

<ng-template *ngIf="prop else loading"> 
     {{prop}} 
    </ng-template> 
    <ng-template #loading> 
    <div class="ajax-loader"> 
     <img src="/assets/ajax-loading-img.gif" > 
    </div> 
    </ng-template> 

또 다른 방법은 수를 수 : 템플릿에서

export class YourComponent implements OnInit { 
    isLoading = false; 

    ngOnInit(){ 
     this.isLoading = true; 
     this.http.get(this.apiendpoint) 
      .do(()=> { // normal case scenario 
       this.isLoading = false; 
      },()=> { // error case scenario 
       this.isLoading = false; 
      }) 
      .subscribe((data) => { 
       // do your operation here. 

      }); 
    } 
    } 

:. 내가 this.http.get (...)`제안

<ng-template *ngIf="isLoading"> 
    <div class="ajax-loader"> 
     <img src="/assets/ajax-loading-img.gif" > 
    </div> 
    </ng-template> 
+0

(할 _ => this.isLoading = 거짓) .subscribe()'. 이 방법은'subscribe' 핸들러에서 두 개의 동일한 행 대신에'do' 핸들러에서 별도의 단계로 토글됩니다. – hlfrmn

+0

예! 네가 옳아. 이것은 이것을하는 좋은 방법입니다. – asmmahmud

+0

npm 패키지 angular2-busy를 사용하는 것은 어떻습니까? –