Angular2 - 如何添加加载微调器?

Fun*_*nky 1 typescript angular

我是Angular 2的新手,我创建了一个调用服务的网站,但需要为每个页面或服务调用添加一个加载gif.有谁知道我怎么能实现这样的东西?

谢谢

zur*_*fyx 5

在您的HTML上,您可以编写如下的微调器:

<span *ngIf="isLoading">Loading...</span>
<span *ngIf="!isLoading">Result</span>
Run Code Online (Sandbox Code Playgroud)

在加载服务之前,可能是在组件加载时,组件上会出现类似的情况:

isLoading: Boolean = true;
Run Code Online (Sandbox Code Playgroud)

调用该组件上的服务,订阅它,然后在订阅完成后将该布尔值更改为true.

myService.subscribe(
  result => {
     // your other stuff here.
     isLoading = false;
  },
  error => {
     isLoading = false;
  }
Run Code Online (Sandbox Code Playgroud)