【问题标题】:to implement a spinner in angular2+在角度 2 中实现微调器
【发布时间】:2018-05-07 18:46:05
【问题描述】:

我正在尝试在我的应用程序中使用微调器,因为有很多数据需要时间来加载。但这是问题所在。我已将此链接作为参考 -

Pre-Bootstrap Loading Screen For Angular2

但是spinner一直不停地在表体中占据了一个固定的位置,它并没有随着数据的完全加载而消失。在上面显示的文章中,他们提到使用

 <app></app> 

 <my-app></my-app> 

 (or probably 

 <app-root></app-root> 

  also)  

Angular 加载完毕后,会自动将“Loading...”替换为真实内容。但是,这不适用于我的程序。当变量为假时,我还使用了一个布尔变量来隐藏。但无济于事。 请检查我的代码并进行相应的指导。

transaction.component.html

 <table>
 <thead> ....
 </thead>

     <my-app>
      <div class="loaded" [class.loaded]="loaded">
      <div class="text-center">
       <i class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
        <div>Something witty for your userbase</div>
       </div>
      </div>
    </my-app>

  <tbody>
        .....
  </tbody>
   <table>

transaction.component.ts

 export class TransactionComponent implements OnInit {
 public loaded=false;


 ngOnInit() {

 this.loaded=true;


  }

 insideSearchByText(filterObj) {

    this.http.post("http://" + url + ":" + port + 
   "/transaction/masterSearchTransactionForm/", this.filterObj
      , { headers: new Headers({ 'Authorization': 'Bearer ' + 
    localStorage.getItem('Token') }) })
   .map(result => this.result = result.json(),

    this.loaded=false)

    .subscribe((res: Response) => {
    ....
    }});
    }

错误

 ERROR Error: Uncaught (in promise): Error: Template parse errors:
 'my-app' is not a known element:
 1. If 'my-app' is an Angular component, then verify that it is part 
 of 
 this module.
 2. If 'my-app' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' 
 to 
 the '@NgModule.schemas' of this component to suppress this message. 
 ("


  [ERROR ->]<my-app></my-app>

【问题讨论】:

  • .loaded 类中有哪些样式?
  • 没有 .loaded 的样式。我使用了很棒的字体作为微调器。加载是我正在使用的布尔变量
  • [class.loaded]="loaded" 表示如果loaded为true,则添加加载的类。
  • ohhh .. 我想如果加载的是真的,那么里面的 div 就可以了。那么我需要添加加载的css吗??
  • 看看我的回答。它应该有帮助。如果没有,我们可以再做一些工作。但是,如果您没有加载样式,则不需要加载该类。 (或者你可能会这样做,这取决于组件的实现)。

标签: angular spinner


【解决方案1】:
<div class="loaded" [class.loaded]="loaded" *ngIf="!loaded">
  <div class="text-center">
    <i class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
    <div>Something witty for your userbase</div>
  </div>
</div>

您只能在加载时显示元素,所以ngIf 应该足够了。

如果加载的类不包含任何内容,您可能不需要包装器。不知道有哪些款式。

如果这对您来说还不够,或者发布您尝试使用的引导组件的链接。

【讨论】:

  • 谢谢湖人 .. 但问题是它只在我第一次加载页面时加载......之后......当页面再次返回时......它不会加载或显示即使我已经在 ngOnInit () 中编写了加载的真值
  • ngIf 根据布尔值显示和隐藏元素。所以每次你开始加载一些东西时,你必须设置this.loaded = false和当它完成时this.loaded = true。 ngOnInit 在创建组件时发生一次。所以我猜你的情况是insideSearchByText(filterObj) { this.loaded = false; ... .subscribe((res: Response) =&gt; { this.loaded = true; ... 我刚刚看到我的应用程序的错误。这意味着您在 AppModule 声明数组中没有该组件。
  • 这意味着我必须创建另一个组件??是吗 ?因为如果我将它添加到这里 --> transaction.component.ts --> @Component({ selector: 'app-transaction',"my-app", --> 它会给出错误 -
  • 您应该使用代码示例创建一个新问题。我们应该在这里谈谈,在那些小消息中。仅使用错误创建问题。从这些组件的 @Component() 装饰器和 AppModule 中放置代码 sn-ps。在这里发个链接,我看看:)
  • 好的,我将创建一个新问题......但是如果没有装饰器内的 ,这段代码是否可以工作
猜你喜欢
  • 2016-05-25
  • 2017-08-08
  • 2021-04-08
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多