I am using Angular 4 for my application development. I need to check If the network connection is available for its have any Connection issue using Angular for. Is it possib
For the required task, There is a npm package, ng-connection-service. Below is the complete code:
import { Component,OnInit } from '@angular/core';
import { ConnectionService } from 'ng-connection-service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'internet-connection-check';
status = 'ONLINE'; //initializing as online by default
isConnected = true;
constructor(private connectionService:ConnectionService){
this.connectionService.monitor().subscribe(isConnected => {
this.isConnected = isConnected;
if(this.isConnected){
this.status = "ONLINE";
} else {
this.status = "OFFLINE"
}
alert(this.status);
});
}
ngOnInit(){
}
}
Also you can find the working complete code over: click here