Create button spinner when clicked in angular 5

CE0

I am trying to create a spinner when button is clicked but it seems its not being run, this is what i did in my component.html

<button type="submit" class="btn  btn-accent btn-block btn-flat b-r-10" style="background: #FF473A;">
  Sign In
  <i class="fa fa-spinner fa-spin" *ngIf="loading"></i>
</button>

and in component.ts

export class LoginComponent implements onInit {
  private loading: boolean;

  //form ngSubmit function()
  login() {
    if (this.user.email, this.user.password) {
      this.loading = true;
      this.userSrv.login(this.user.email, this.user.password); //api call here
      this.loading = false;
    }
  }
}

okay my service.ts

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import { Router } from '@angular/router';
import { Globals } from '../shared/api';
import 'rxjs/add/operator/toPromise';
import { Observable } from 'rxjs/Observable';

@Injectable() export class UserService {

private loginUrl = this.globals.LOGIN_URL;
private loggedIn = false;
public loading: boolean = false;


constructor(private http: Http, private router: Router, private globals: Globals) { }

login(email: string, password: string) {
    let headers = new Headers();
    this.loading = true;
    headers.append('Content-Type', 'application/json');
    return this.http.post(this.loginUrl, JSON.stringify({ email, password }), { headers })
        .subscribe(res => {
            let data = res.json();

            if (data.token) {
                this.loading = false;
                window.location.href = '/';
            }
            else {
                this.loading = false;
                this.router.navigateByUrl('/login');
            }

        }, error => {
            this.loading = false;
        })
};

and this is what i hope to achieve when clicked

enter image description here

Vikas

HttpModule is deprecated you must use the new HttpClientModule whcih by default formats the response to JSON so we no longer need to parse it using response.json():

Revised service code: Return Observable from your service and subscribe it in your component

    import { Injectable } from '@angular/core';
    import { HttpHeaders,HttpClient } from '@angular/common/http';
    import { Router } from '@angular/router';
    import { Globals } from '../shared/api';

    import { Observable } from 'rxjs/Observable';
    private loginUrl = this.globals.LOGIN_URL;
    private loggedIn = false;
    public loading: boolean = false;


    constructor(private http: HttpClient, private globals: Globals) { }

    login(email: string, password: string):Observable<any> {
       const headers = new HttpHeaders({'Content-Type':'application/json;})

    return this.http.post(this.loginUrl, JSON.stringify({ email, password }), { headers });
}

Revised Component:
Put the logic inside susbscribe call back to ensure that spinner goes off only when your request is resolved.

   export class LoginComponent implements onInit {
      constructor(private router:Router){}
      public loading: boolean;

      //form ngSubmit function()
      login() {
        if (this.user.email, this.user.password) {
          this.loading = true;
          this.userSrv.login(this.user.email, this.user.password).subscribe(res 
                => {
            let data = res;

            if (data.token) {
                this.loading = false;
                window.location.href = '/';
            }
            else {
                this.loading = false;
                this.router.navigateByUrl('/login');
            }

        }, error => {
            this.loading = false;
        });
}}

revised HTML

<button type="submit" class="btn  btn-accent btn-block btn-flat b-r-10" (click)="login()" style="background: #FF473A;">
      Sign In
      <i class="fa fa-spinner fa-spin" *ngIf="loading"></i>
    </button>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In angular 4 how can I make a reuseable button component which can enable a spinner when clicked

Spinner loads for every button when clicked on a specific button

How can create fireworks when a button is clicked

How to create alert dialog on button when it is clicked?

Create image when button is clicked JS

Create a +1 Animation when button is clicked

change colour of a mat button when clicked in angular

Page not refreshed when clicked delete button in angular

fire angular pipe event when button is clicked

How to download a file from a url when a button is clicked in angular5 / 6

create a submit button with spinner

Spinner not changing when value is clicked

Angular - Make a button active when it is clicked and inactive when an other button in the group is clicked

How to show spinner in Stripe after button is clicked?

How to create a new label when a button is clicked in javafx and scenebuilder?

How can I create a button that increments a counter when clicked?

How do I create a button that changes sprites when clicked in pygame?

How to create a button that shows some inputs only when clicked/toggled?

UI is freezed for 5 seconds when async button is clicked at the first time

Bootstrap 5 changing the text on a collapse button when clicked

identify clicked button in Angular

How do I change the text of a button when clicked, in Angular

Angular Changing Row Color when Preview Button is Clicked

Router.navigate not redirecting when button is clicked - Angular 9

Angular ngModelOptions - bind value to model only when button is clicked

angular material2 change button color when clicked

want to get details of a specific user when clicked on button in Angular 4

Click of a button not working when clicked through modal in angular

How to !click one button when another is clicked angular 2?