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
Detecting internet connection status in Angular application
We can achieve desired feature using window.ononline and window.onoffline events.
You can install service via npm command:
npm i ng-connection-service
Subscribe to monitor() method to get push notification whenever internet connection status is changed
import { Component } from '@angular/core';
import { ConnectionService } from 'ng-connection-service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
status = 'ONLINE';
isConnected = true;
constructor(private connectionService: ConnectionService) {
this.connectionService.monitor().subscribe(isConnected => {
this.isConnected = isConnected;
if (this.isConnected) {
this.status = "ONLINE";
}
else {
this.status = "OFFLINE";
}
})
}
}