What is Routing ?
Routing in AngularJS allows your application becomes a Single Page Application (SPA). In your application, you have to navigate to different pages, at the same time your application should be act as SPA without page reload. This is called as Routing.
For the Routing, here we use ngRoute Module. This module routes our applications to different pages without page refreshment.
How to Implement Routing in your Application ?
- At first you need to include the routing library file.
- Next include ngRoute dependency in your application module.
var app = angular.module(“myApp”, [“ngRoute”]);
Now your application ready to access Routing.
How to configure Routing in your Application ?
Once you done dependency injection, You can use $routeProvider service. We know that $routeProvider is a default Provider and we can inject that into configuration phase.
templateUrl : “index.html”,
templateUrl : “aboutus.html”,
templateUrl : “service.html”,
templateUrl : “contactus.html”,
Once you inject $routeProvider in config phase, you are ready to use routing. Then inbuilt method when has 2 properties
- The url query string and
- Object which has properties of
- template – You can do your html designs here. Its not a new page.
- templateUrl – This is a new page which you going to call when your query string meets the right value.
- controller – This controller will be attached on the template and you don’t need to attach the controller manually again.
- the otherwise method call the template while the query string matches none of the value you mentioned in the config phase.
Where the templates goes to load ?
We need a container to receive the templates which is provide by Routing.
We can set the container in three different ways.
- As an Element – <ng-view></ng-view>
- As an Attribute – <div ng-view></div>
- As a Class – <div class=”ng-view”>
For a standard we use as an Element.
So the templates dynamically loads on the <ng-view> </ng-view> directive based on the query string.
To know more about the complete topics of AngularJS, please visit AngularJS Training in Chennai.