Single page application using AngularJS

AngularJS logo

Single page application is superior in terms of performance. It decreases load time of pages by storing the functionality, once it is loaded the first time. If you need to update small parts of page, it’s faster to focus only on these views and their data rather than reloading entire page.

Some of the key benefits of single page application

  • Provides more native like experience.
  • Less bandwidth and faster navigation.
  • Load all markup in initial load.
  • Insert and remove HTML on demand.
  • Prevent the browser from requesting URL from server.

How to create single page app using AngularJS

Any Application must do two things to start Angular:

  1. Load the angular.js library.
  2. Tell Angular which part of the DOM it should manage with ng-app directive.

Download the source code or View the Demo

ng-app directive

To bootstrap the application use custom ng-app HTML attribute. This tells Angular which part of the page it should manage. In the demo, I have placed it in the HTML element, where I am telling Angular that, I want to manage the whole page. If you want to use AngularJS with other library. You can use it in a specific section or div by placing ng-app directive in that div.

ng-controller directive

In Angular app, you can manage areas of the page with javaScript classes called controllers. Including controller in the body, will manage everything between body. Controller primarily initialize the $scope object which wire model and view.

Module and Controller creation

Module and controller are created in a separate layer that keep the HTML page clean. In the demo, all the controllers are placed in “script.js”

Creation of different controllers for different pages

In the demo application different controllers have been created for different pages – spHomeController, spAboutController and spContactController. Each controller has its data to place in the respective page.

Wiring different controllers and pages

Different controllers for different pages have been created. Now, it needs to be wired together. Routing is taken care by service provider that Angular provides $routeProvider. This service wire together controllers, view, and the current URL location in the browser. We can use config() method to configure $routeProvider and this $routeProvider provides method .when() and .otherwise(), which can be used to wire the controllers and pages.

Injecting active class to highlight the current page tab

Angular provides the different services that can be used, as and when required. We use AngularJS’s dependency injection to inject a service object in our controller, and Angular uses $injector to find corresponding service injector. To apply the active class on the current page tab we need to find the current URL. The $location services parses the URL in the browser address and make the URL available in the application. In the demo file isActive() function of controller with name spController is using the $location service.

Using ng-class to highlight the current page tab

ng-class directive allows to dynamically set CSS classes on an HTML element. In the HTML page now we can call isActive() function created in the controller name spController.

Download the source code or View the Demo

Why choose AngularJS, there are other frameworks

Every framework has its pros and cons, but it depends on the requirement, what kind of application is being developed. If application required heavy DOM manipulation then combination of backbone and jquery is right, but if the application is very data-driven, then AngularJS has the nice data binding. Here, are a few points in favor of AngularJS over other framework.

  • Template support without any 3rd party template engine.
  • Nested template support without any dependency.
  • Two-way data binding.
  • It doesn’t required any third-party dependency.
  • MVW (Model View Whatever) framework.

Author: Gopal Juneja

A UI/UX enthusiast living in India Delhi having 12+ years of industry experience to use in beautiful design and handsome HTML coding.

2 thoughts on “Single page application using AngularJS”

Leave a Reply

Your email address will not be published.