【问题标题】:How to stop the spinner after the loading finishes in an angular app?在角度应用程序中加载完成后如何停止微调器?
【发布时间】:2018-02-22 07:47:34
【问题描述】:

我正在实现一个微调器,它最初工作正常,但即使在页面加载后它也会继续旋转。

loader.service.ts

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

@Injectable()
export class LoaderService {
    public status: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);

    display(value: boolean) {
        this.status.next(value);
    }
}

app.module.ts

导入 LoadService 并将其添加到 providers 数组中

app.component.html

我正在检查 showLoader 的值,看看它是真还是假,因为 true 会显示加载程序,而 false 不会。

<router-outlet>
   <span *ngIf="showLoader" class="loading"></span>{{showLoader}}
</router-outlet>

app.component.ts

导入的LoaderService

export class AppComponent {
  //for the spinner
  showLoader: boolean;
  //LoaderService is for the spinner
  constructorprivate loaderService: LoaderService) { }
  //for the spinner
  ngOnInit() {
    this.loaderService.status.subscribe((val: boolean) => {
      this.showLoader = val;
    });
  }
}

custom.component.ts

@Component({
  selector: 'app-custom',
  templateUrl: './custom.component.html',
  styleUrls: ['./custom.component.css']
})
export class CustomComponent implements OnInit {

    //LoaderService is for the spinner
    constructor(private loaderService: LoaderService) { }

    ngOnInit() {
        //http call starts
        this.loaderService.display(true);
    }

    ngAfterViewInit() {
        d3.json("https://...", function(data) {
            createChart(data);
        });
        function createChart(data) {
            ...

            dc.renderAll();
        }//end of function  
    }//end of ngAfterView
}//end of export class

我正在展示一些 dc 图表,我希望在图表显示后停止微调。

我需要使用this.loaderService.display(false); 停止微调器,但在 dc.renderAll(); 之后立即使用它将 showLoader 的值显示为 false,因此不会出现微调器。

我们将不胜感激任何形式的帮助。谢谢。

【问题讨论】:

    标签: javascript angular typescript progress-bar dc.js


    【解决方案1】:

    一种选择是在您的 AppComponent 中为 showLoader 添加一个默认值。 您可以将其设置为 true 作为默认值,使用 showLoader: boolean = true;

    export class AppComponent {
      //for the spinner
      showLoader: boolean = true;
      //LoaderService is for the spinner
      constructorprivate loaderService: LoaderService) { }
      //for the spinner
      ngOnInit() {
        this.loaderService.status.subscribe((val: boolean) => {
          this.showLoader = val;
        });
      }
    }
    

    默认情况下会显示微调器。 然后就可以这样设置了。

    custom.component.ts

      ngAfterViewInit() {
        let loaderService = this.loaderService;
        d3.json("https://...", function(data) {
            createChart(data);
        });
        function createChart(data) {
            ...
    
            dc.renderAll();
            loaderService.display(false);
        }//end of function  
      }//end of ngAfterView
    

    【讨论】:

    • 您能详细说明一下吗?我不太明白你在说什么。
    • showLoader: boolean;替换为showLoader: boolean = true;
    • this.loaderService.display(false); 在 custom.component.ts 中的什么位置?
    • 没错。它默认打开,当你想显示的内容加载时隐藏。
    • this.loaderService.display(false); 在 custom.component.ts 中的什么位置?
    【解决方案2】:

    如果您理解正确,dc.renderAll() 是一个异步函数,因此您需要重新构建您的应用程序以便隐藏微调器 AFTER 该函数实际上已结束处理。

    一种方法是将 renderAll() 转换为 observable 并订阅它,在订阅中调用 loaderService.display(false)。

    dc.renderAll().subscribe(
      value => this.loaderService.display(false)
    )
    

    【讨论】:

    • 我收到错误无法读取未定义的属性“订阅”。
    • 你应该重构你的 renderAll 函数,让它返回一个 Observable。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多