AngularJS tips and tricks – broadcast online and offline status

Mr. D:

AngularJS tips and tricks – broadcast online and offline status

Originally posted on Terkildsen's Blog:

In continuing my experiments with AngularJS, I often stumble upon new challenges, I haven’t met before. One of these challenges was to broadcast the online and offline status of the browser.

I wanted to be able to hide different parts of the gui, if the user wasn’t online. In order to do that, I had to create a centralized variable, that would notify the gui whenever changes occurred. I did this by applying the following code in my app:

As you can see, the function adds an event listener that will automatically update the $ variable. Now, I can just use this variable in my HTML – like so:

Works like a charm :-)

View original