YouTube Icon

How to detect network connection is Online/Offline Status In Angular

How to detect network connection is Online/Offline Status In Angular

We all might be familiar with the JavaScript way of checking Online/ Offline status. But in the case of Angular, we need to duly unsubscribe the events we're harkening to, else, we might beget gratuitous actions and memory leaks. 

Plain JavaScript

window.addEventListener("load", () => {
  this.internetStatus = navigator.onLine

  window.addEventListener("online", () => {
    this.internetStatus = true

  window.addEventListener("offline", () => {
    this.internetStatus = false


  • app.component.html
<h1>Internet Status</h1>

  class="widget-content-online header-user-info ml-3"
  &#9679; Online
  class="widget-content-offline header-user-info ml-3"
  &#9679; Offline
  • app.component.ts
import { Component, OnDestroy, OnInit, VERSION } from '@angular/core';
import { fromEvent, merge, of, Subscription } from 'rxjs';
import { map } from 'rxjs/operators';

  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
export class AppComponent implements OnInit, OnDestroy {
  internetStatus: boolean = false;
  internetStatus$: Subscription = Subscription.EMPTY;

  constructor() {}

  ngOnInit(): void {

  ngOnDestroy(): void {

  checkInternetStatus() {
    this.internetStatus = navigator.onLine;
    this.internetStatus$ = merge(
      fromEvent(window, 'online'),
      fromEvent(window, 'offline')
      .pipe(map(() => navigator.onLine))
      .subscribe(status => {
        console.log('status', status);
        this.internetStatus = status;
  • styles.css
.widget-content-online {
	padding: 3px 11px 5px 11px;
	border-radius: 52px;
	width: 90px;
	background: rgba(67, 164, 90, 0.15);
	font-size: 15px;
	color: #43A45A;
	text-align: center;

.widget-content-offline {
	padding: 3px 11px 5px 11px;
	border-radius: 52px;
	width: 90px;
	background: rgba(165, 83, 83, 0.15);
	font-size: 15px;
	color: #e52525;
	text-align: center;

Here you can see the Demo:

Here you check the code:

Follow on youtube channel for more videos: Youtube Channel

Here you can see youtube video: Detect network connection is Online/Offline Status Using Angular

Author Biography.

Lokesh Gupta
Lokesh Gupta

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working in the information technology and services industry. I enjoy solving complex problems within budget and deadlines putting my skills on PHP, MySQL, Python, Codeigniter, Yii2, Laravel, AngularJS, ReactJS, NodeJS to best use. Through Knowledge of UML & visual modeling, application architecture design & business process modeling. Successfully delivered various projects, based on different technologies across the globe.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.


CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.


Our mission is "Har Koi Dekhe Video, Har Ghar Dekhe Video, Ghar Ghar Dekhe Video" so we Provide videos related to Tutorials, Travel, Technology, Wedding, Cooking, Dance, Festivals, Celebration.

Apna Video Wala

News & Blogs


How to Get User Country Name, Browser, OS Name,...

Using PHP get stoner details IP address country, device, cybersurfer, operating system  &nb...


How to Drag And Drop Data Between Two Tables In...

Introduction  occasionally you need to drag and drop data from one table to another table...


Top 10 Amazing Books For Every Designers

There are innumerous books out there that can help you come a better developer, whether you are l...

Top Authors

Lamia Rochdi is the Marketing Manager at Bell Flavors & Fragrances EMEA. A successful family-...

Lamia Rochdi

I’m Mertin Wilson a technician in a camera company and certified expert of different P...

Mertin Wilson

Zakariya has recently joined the PakWheels team as a Content Marketing Executive, shortly after g...

Zakariya Usman

Pankaj Singh is a Senior Digital Marketing Consultant with more than 2 years of experience in SEO...

Pankaj Singh

Our Client Says

WhatsApp Chat with Our Support Team