February 1, 2015 6:40 pm

How to do Routing and Views in AngularJs

In this tutorial we will go through few more important features in AngularJs called Routing and Views. Once we develop the application with more features it will grow and tough to manage all the templates of views and hard to manage. So it will be good if we divide the whole application into multiple views and load them using routing makes application more logical and easily manageable.

Routing and Views in AngularJs

Routing helps you in dividing your application into different views and bind them to the controllers.

angularjs-routing-view-controller

Let’s see the above example it tells us there are two routes, each one pointing to different view and binding to their respective controller. let’s routing and views in more detail.

Introduction to $routeProvider

The magic of Routing is taken care by a service provider that Angular provides called $routingProvider. This service providers are set of functions, when these instantiated the will contain a property called $get, which holds of the service factory function.

When we use AngularJs dependency injection and inject a service object in our Controller, Angular uses $injector to find corresponding service injector. Once it get a hold on service injector, it uses $get method of it to get an instance of service object. Sometime the service provider needs certain info in order to instantiate service object.

Application routes in Angular are declared via the $routeProvider, which is the provider of the $route service. This service makes it easy to wire together controllers, view templates, and the current URL location in the browser. Using this feature we can implement deep linking, which lets us utilize the browser’s history and bookmarks.

Syntax to add Routing

Let’s see how we can add routing and views to an angular application.

We defined an angular app called “phonecatApp” using angular module method. Once we have our app, we can use config() method to configure $routeProvider. $routeProvider provides method when() and otherwise() which we can use to define the routing for our app. We defined two urls /phones and /phone/:phoneId and mapped them with the respective views templates/phone-list.html and templates/phone-detail.html. When we open the urls they will invoke respective controllers.

Hello World using Routing

Let’s go through an example in Angularjs and use routing to load different templates at runtime.

The $route service is usually used in conjunction with the ngView directive. The role of the ngView directive is to include the view template for the current route into the layout template.

Let’s define controllers in js/app.js file like below to bind our view templates.

Now we have to add HTML template files let’s see phone-list.html view.

Let’s see phone-detail.html view

In the above example we saw dynamic URL’s also by passing a parameter through routing like below.

and we can read the parameter in controller function like below.

This is the way you have to build your routing and also link with different views by binding them to controllers. For more examples please look at AngularJs Developer tutorial.

Thanks

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:
  • kamran mirza

    keep up the great work brother