如何在角度6中显示微调器

时间:2018-06-23 10:27:01

标签: angular angular-material spinner

我不熟悉语言和Web开发,但是能够设计各种Web页面,并使用HTTP客户端模块从服务器获取数据。

从服务器获取数据时,我想显示进度微调器,但无法执行。我已经搜索过谷歌,但是没有什么让我去做。请帮助我实现这一目标。

代码:

login.component.ts

 userLogin() {
        console.log('logging in');
        this.eService.signIn(this.user_name, this.password)
        .subscribe(
            data => {
                console.log(data);
               this.admin = data;
               if ( this.admin.firstLogin === true) {
                   // go to update admin password
               } else {
                this.router.navigate(['/dashboard']);
                }
               localStorage.setItem('isLoggedin', 'true');
             }
        );
    }

login.html

<div class="login-page" [@routerTransition]>
    <div class="row justify-content-md-center">
        <div class="col-md-4">
            <img src="assets/images/logo.png" width="150px" class="user-avatar" />
            <h1>Users/h1>
            <form role="form">
                <div class="form-content">
                    <div class="form-group">
                        <input type="text" name="username" [(ngModel)]="user_name" class="form-control input-underline input-lg" id="" placeholder="username">
                    </div>

                    <div class="form-group">
                        <input type="password" name="password" [(ngModel)]="password" (keyup.enter)="userLogin()" class="form-control input-underline input-lg" id="" placeholder="password">
                    </div>
                </div>
                <a class="btn rounded-btn" (click)="userLogin()"> Log in </a>
                &nbsp;
                <a class="btn rounded-btn" >Clear</a>
            </form>
        </div>
    </div>
</div>

因此,当我请求登录服务时,我想向微调器显示,请帮助我,该怎么办?

我知道这对这里的许多开发人员都很简单,但是对我来说,这变得有些困难了。

4 个答案:

答案 0 :(得分:5)

将微调框添加到您的HTML代码中,如下所示:

<img *ngIf="loading" src="assets/my-spinner.gif" /> <!-- Or use a CSS one if you like -->

然后,在Typescript中,您需要创建一个名为loading的变量,并按如下所示进行设置:

userLogin() {
    console.log('logging in');
    this.loading = true; // Add this line
    this.eService.signIn(this.user_name, this.password)
    .subscribe(
        data => {
            console.log(data);
           this.loading = false; // And this one
           this.admin = data;
           if ( this.admin.firstLogin === true) {
               // go to update admin password
           } else {
            this.router.navigate(['/dashboard']);
            }
           localStorage.setItem('isLoggedin', 'true');
         }
    );
}

在服务运行期间,这会将loading设置为true

答案 1 :(得分:4)

您可以使用ng4-loading-spinner

执行npm i ng4-loading-spinner --save

将模块导入您的应用程序根模块

import { Ng4LoadingSpinnerModule } from 'ng4-loading-spinner';

输入一个条目

 imports: [ Ng4LoadingSpinnerModule.forRoot() ]

将微调器组件包括到根级别组件中。

<ng4-loading-spinner> </ng4-loading-spinner>

show()回调中使用hide()subscribe

import { Ng4LoadingSpinnerService } from 'ng4-loading-spinner';
 constructor(
        private spinnerService: Ng4LoadingSpinnerService
    ) { }
    userLogin() {

        this.spinnerService.show();
            console.log('logging in');
            this.eService.signIn(this.user_name, this.password)
            .subscribe(
                data => {
                    console.log(data);
                   this.admin = data;
                   if ( this.admin.firstLogin === true) {
                       // go to update admin password
                   } else {
                    this.router.navigate(['/dashboard']);
                    }
                   localStorage.setItem('isLoggedin', 'true');
                 },
               ()=>this.spinnerService.hide();
            );
        }

Live Demo

答案 2 :(得分:0)

对于仍在搜索角向加载微调器的人,请随时关注:

答案 3 :(得分:0)

添加此 HTML 代码

<div class="spinner-border" role="status" *ngIf="your_condition">
      <span class="sr-only" id="loadbar"></span>
</div>

在 .css 文件中(添加必要的样式),

#loadbar{
  animation: spin 2s linear infinite;
  }

@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}

set the condition to true or false as per your code in .ts file