简体   繁体   English

Angular2在等待数据加载时更新视图

[英]Angular2 Update a View while waiting for data to load

I have a form that requires input asking a user for numbers to pick. 我有一个要求输入要求用户选择号码的表格。 When the form is submitted, I want a "spinner graphic" to display while the data is being generated and formatted. 提交表单后,我希望在生成和格式化数据时显示“旋转图形”。
At the moment, I cannot get the spinner to display until after the data is returned. 目前,直到返回数据后,我才能显示微调器。

My code looks like : 我的代码如下:

getTickets(){
  this.PBPicks=this._lottoService.getPBTicket(this.newTixForm.value['PBTix']);
  this.MegaPicks=this._lottoService.getMegaTicket(
  this.newTixForm.value['MegaTix']);
  return false;
 }

submitForm(){
  this.isLoading=true;
  console.log('Show Spinner');
  this.isLoading=this.getTickets();
    console.log("Data Loaded");
 }

When the data is returned and displayed is when the spinner shows and then disappears even though the console shows it being executed much sooner than when the data is returned. 返回并显示数据的时间是微调器显示然后消失的时间,即使控制台显示该数据的执行时间比返回数据时要早得多。

Here is what I did: Loading icon component: 这是我所做的:加载图标组件:

isLoading = false;
   constructor() { }

   ngOnInit() {}
   show() 
  {
    this.isLoading = true;
   }
  hide() 
  {
    this.isLoading = false;
  }

Loading-icon.component.html Loading-icon.component.html

<span *ngIf="isLoading">
  <img src="/assets/images/loading.gif" alt="">
</span>

App.Component.ts App.Component.ts

//To get access to component and its method, using the @ViewChild decorator
 @ViewChild('imLoading') imLoading: LoadingIconComponent;

this.model.getData = getData;
this.imLoading.show();

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

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