I am new to angular and web development but able to design various web pages and got data from the server using HTTP client module.
While getting data from server I want to show progress spinner, but I am not able to do it. I have searched google but nothing made me to do it. Please help me to achieve this.
Code:
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>
<a class="btn rounded-btn" >Clear</a>
</form>
</div>
</div>
</div>
So when I am requesting signIn service I want to show the spinner, Please help me, how can I do it?
I know this is simple for many of the developers here, but for me It is getting somewhat tough.
You can use ng4-loading-spinner
Execute npm i ng4-loading-spinner --save
Import module to your application root module
import { Ng4LoadingSpinnerModule } from 'ng4-loading-spinner';
Make an import entry
imports: [ Ng4LoadingSpinnerModule.forRoot() ]
Include spinner component to your root level component.
<ng4-loading-spinner> </ng4-loading-spinner>
use show()
and hide()
inside subscribe
callback
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
Add the spinner into your HTML code, like so:
<img *ngIf="loading" src="assets/my-spinner.gif" /> <!-- Or use a CSS one if you like -->
Then, in your Typescript, you need to create a variable called loading
, and set it like so:
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');
}
);
}
This will set loading
to true while the service is in action
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With