简体   繁体   English

如何在API调用上显示和隐藏Mat Spinner

[英]How to show and hide mat spinner on API call

I was trying to show mat spinner on API method call. 我试图在API方法调用上显示Mat Spinner。 Below is my API call code on which I subscribe to get the data, Usually it is just 2 min work but this time didn't work, also I know this happens due to a subscribe method so any easy way to implement that? 以下是我订阅以获取数据的API调用代码,通常只有2分钟的工作时间,但是这次却不起作用,我也知道这是由于有一个Subscribe方法导致的,因此有什么简单的实现方法吗?

 this.loading = true;

 this.ticketModelService.farmerList
  .subscribe(value => {
    if (value) {
      this.farmerList = value.data;
      this.paginationNumbers = value.recordsFiltered
    }
  })

  this.loading = false;

EDIT: Empty screen for 2 ms. 编辑:2秒钟的空白屏幕。 在此处输入图片说明

I think your issue is your spinner appears and suddenly disappears. 我认为您的问题是您的微调框出现了,突然消失了。 So here your loading variable will not wait for API to process and it will disappear within ms. 因此,这里的加载变量不会等待API处理,并且会在ms内消失。 So you should hide your loader in your API callback. 因此,您应该在API回调中隐藏加载程序。 So until you're API is processing, loader will stay there. 因此,在您处理API之前,加载程序将一直存在。 And after getting response you can hide you're loader even if it's an error. 得到响应后,即使出现错误,您也可以隐藏自己是加载程序。

 this.loading = true;

 this.ticketModelService.farmerList
  .subscribe(value => {
     this.loading = false;
    if (value) {
      this.farmerList = value.data;
      this.paginationNumbers = value.recordsFiltered
    }
  })

Update your code as above. 如上所述更新代码。

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

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