简体   繁体   English

在angular4应用程序中加载数据时显示进度条

[英]Display progress spinner while datas are loading in angular4 application

I have an angular 4 application and I get datas from a database with a springboot application. 我有一个angular 4应用程序,我使用springboot应用程序从数据库中获取数据。 When I open my application, there is a get which returns data to display on the first page. 当我打开应用程序时,有一个get返回数据以显示在第一页上。 So, I want to display a progress spinner while the datas are loading and when the datas are loaded, remove the spinner and display the datas. 因此,我想在加载数据时显示进度条,并在加载数据时,删除旋转条并显示数据。

So, I don't know how to do that. 所以,我不知道该怎么做。 For now, this is my html code (the datas are displayed in visTimeline : 现在,这是我的html代码(数据显示在visTimeline

 <md-spinner id="isLoadingSpinner" style="margin:0 auto;"></md-spinner>
 <visTimeline (myTask)="returnTask($event)" [tasks]="tasks"></visTimeline>

And the ts code : 和ts代码:

    ngOnInit() {
        this.tasks = this.datasService.getTasks();
        if(this.datasService.isLoading == false){
            document.getElementById("isLoadingSpinner").style.display = "none";
        }
    }

And this is the get in the datas.service : 这是datas.service中的获取:

    getAffectationsFromDataBase() {
  this.http.get('http://localhost:8081/affectation/findAll').map((response:Response) => {
        this.tasks.tasks = this.transformAffectations(response.json().affectations);
    }).subscribe(result =>  this.isLoading = false);
}

So, I try with if in the ngOnInit function and with a while but it doesn't work. 因此,我尝试在ngOnInit函数中使用if并稍等片刻,但是它不起作用。 Do you know how I can do that ? 你知道我该怎么做吗?

The reason why it does not work is because the ngOnInit() method is synchronous and so it runs on the initialization of the page without waiting for any response. 之所以不起作用,是因为ngOnInit()方法是同步的,因此它在页面初始化时运行而无需等待任何响应。

You can use the [hidden] directive or *ngIf to solve this problem with the condition being the boolean from your "datasService" class. 您可以使用[hidden]指令或* ngIf来解决此问题,条件是“ datasService”类中的布尔值。 Adjust your HTML code in one of the following ways: 通过以下方式之一调整HTML代码:

 <md-spinner [hidden]="!datasService.isLoading" id="isLoadingSpinner" style="margin:0 auto;"></md-spinner>

or 要么

<md-spinner *ngIf="datasService.isLoading" id="isLoadingSpinner" style="margin:0 auto;"></md-spinner>

With one of these implementations you don't need to check the condition in ngOnInit(); 使用这些实现之一,您无需检查ngOnInit()中的条件;

I wrote a post here asking about how to do something in Ionic, based on the way you do things in Angular here . 我写了一张贴在这里询问如何做一些离子,根据您的角度做事情的方式在这里 It covers your needs. 它满足您的需求。 Router Data Resolvers 路由器数据解析器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM