AngularJS View And Routing

AngularJS is a framework that is used for develop the single page application, so if we add more and more logic to an app, it will be very difficult to manage the application. Dividing the application in multiples Views and using Routing to load different part of application is the solution of this problem making it more manageable. To do this AngularJS has provided ng-view and ng-template directives and $routeProvider services. Routing is used to divide your application in logical views and bind different views to Controllers.

Example:

  1. <html>  
  2.   
  3.     <head>  
  4.         <title>Angular JS Views</title>  
  5.         <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">  
  6.             </script>  
  7.             <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js">  
  8.                 </script>  
  9.                 <style>  
  10.                 a {  
  11.                     margin-right: 20px;  
  12.                 }  
  13.                  
  14.                 #div1 {  
  15.                     background-color: aqua;  
  16.                     width: 500px;  
  17.                 }  
  18.                  
  19.                 #div2 {  
  20.                     background-color: #7085EF;  
  21.                     width: 500px;  
  22.                 }  
  23.                  
  24.                 #div3 {  
  25.                     background-color: #7BEF70;  
  26.                     width: 500px;  
  27.                 }  
  28.             </style>  
  29.     </head>  
  30.   
  31.     <body>  
  32.     <h2>AngularJS Routing and View Application</h2>  
  33.     <divng-app="myApp">  
  34.         <ahref="#viewDelhi">View Delhi  
  35.         </a>  
  36.         <ahref="#viewMumbai">View Mumbai  
  37.         </a>  
  38.         <ahref="#viewJaipur">View Jaipur  
  39.         </a>  
  40.         <divng-view>  
  41.         </div>  
  42.         <scripttype="text/ng-template" id="delhi.htm">  
  43.             <div id="div1">  
  44.                 <h2> Delhi </h2>  
  45.                 <img src="delhi.png" />  
  46.                 <br/> {{message}}   
  47.             </div>  
  48.         </script>  
  49.         <script type="text/ng-template" id="mumbai.htm">  
  50.             <div id="div2">  
  51.                 <h2> Mumbai </h2>  
  52.                 <imgsrc="mumbai.png" />  
  53.                 <br/> {{message}}   
  54.             </div>  
  55.         </script>  
  56.         <script type="text/ng-template" id="jaipur.htm">  
  57.             <div id="div3">  
  58.                 <h2> Jaipur </h2>  
  59.                 <imgsrc="jaipur.png" />  
  60.                 <br/> {{message}}   
  61.             </div>  
  62.         </script>  
  63.     </div>  
  64.     <script>  
  65.        var mainApp = angular.module("myApp", ['ngRoute']);  
  66. mainApp.config(['$routeProvider', function ($routeProvider)  
  67. {  
  68.     $routeProvider.  
  69.     when('/viewDelhi',  
  70.     {  
  71.         templateUrl: 'delhi.htm',  
  72.         controller: 'AddDelhi'  
  73.     }).  
  74.     when('/viewMumbai',  
  75.     {  
  76.         templateUrl: 'mumbai.htm',  
  77.         controller: 'AddMumbai'  
  78.     }).  
  79.     when('/viewJaipur',  
  80.     {  
  81.         templateUrl: 'jaipur.htm',  
  82.         controller: 'AddJaipur'  
  83.     }).  
  84.     otherwise(  
  85.     {  
  86.         redirectTo: '/viewDelhi'  
  87.     });  
  88. }]);  
  89. mainApp.controller('AddDelhi', function ($scope)  
  90. {  
  91.     $scope.message = "Delhi, India’s capital territory, is a massive metropolitan area \  
  92. in the country’s north. In Old Delhi, a neighborhood dating to the 1600s, \  
  93. stands the imposing Mughal-era Red Fort, a symbol of India, and the sprawling, \  
  94. Jama Masjid mosque, whose courtyard accommodates 25,000. Nearby is ChandniChowk, \  
  95. a vibrant bazaar filled with food carts, \  
  96. sweets shops and spice stalls ";  
  97. });  
  98. mainApp.controller('AddMumbai', function ($scope)  
  99. {  
  100.     $scope.message = "Mumbai City District is a district of Maharashtra in KonkanDivision.As a city district,\  
  101. it has no headquarters or subdivisions. makes up the metropolis of Mumbai. ";  
  102. });  
  103. mainApp.controller('AddJaipur', function ($scope)  
  104. {  
  105.     $scope.message = "Jaipur, the capital of India’s Rajasthan state, evokes the royal family\  
  106. that once ruled the region and that, in 1727, \  
  107. founded what is now called the Old City, or Pink City for its trademark building color.At the center of its stately street grid (notable in India) stands the  opulent,\  
  108. collonaded City Palace complex, \  
  109. today houses several museum collections of textiles and art ";  
  110. }); < /script> < /body> < /html>  
  111.                                           
Output:

See output

result

run

In above example we create a view and three different templates for view and at last we configured the roots for these templates. Now we divide the above code section in multiple sections and read above each part.

ng-view

ng-view tag simply creates a place holder where a corresponding view (html or ng-template view) can be placed based on the configuration. Every time the current route changes, the included view changes with it according to the configuration of the $route service.

Example:

 

  1. <divng-app="myApp">  
  2.    <divng-view></div>  
  3. </div>  
ng-template

The ng-template directive is used to create an html view using script tag. In our example we create three templates. Each templates contains "id" attribute which is used by $routeProvider to map a view with a controller. A template is like a html page itself, in a template we add any html element.

Example:
  1. <scripttype="text/ng-template"id="delhi">  
  2.     <div id="div1">  
  3.         <h2> Delhi </h2>  
  4.         <imgsrc="delhi.png" />  
  5.         <br/>  
  6. {{message}}  
  7.   
  8.     </div>  
  9. </script>  
  10. <scripttype="text/ng-template"id="mumbai">  
  11.     <div id="div2">  
  12.         <h2> Mumbai </h2>  
  13.         <imgsrc="mumbai.png" />  
  14.         <br/>  
  15. {{message}}  
  16.   
  17.     </div>  
  18. </script>  
  19. <scripttype="text/ng-template"id="jaipur">  
  20.     <div id="div3">  
  21.         <h2> Jaipur </h2>  
  22.         <imgsrc="jaipur.png" />  
  23.         <br/>  
  24. {{message}}  
  25.   
  26.     </div>  
  27. </script>  
$routeProvider

The $routeProvider is used to set the configuration of urls and map them with the corresponding html page or ng-template and also attach a controller. Routing in AngularJS is taken care by a service provide that is called $routeProvider. Routes for templates and urls in Angular are declared via the $routeProvider, that 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.

We can use config() method of “myApp” module to configure $routeProvider. The when method of $routeProvideris used to bind the url with a template. This method take a url(i.e. “/viewDelhi”) that will map with a template (i.e. delhi.htm) using the templateUrl parameter. The when method also bind a controller for template using the controller parameter (i.e. controller: 'AddDelhi'), otherwise method is used to set the default view.

Example:
  1. mainApp.config(['$routeProvider', function ($routeProvider)  
  2. {  
  3.     $routeProvider.  
  4.     when('/viewDelhi',  
  5.     {  
  6.         templateUrl: 'delhi',  
  7.         controller: 'AddDelhi'  
  8.     }).  
  9.     when('/viewMumbai',  
  10.     {  
  11.         templateUrl: 'mumbai',  
  12.         controller: 'AddMumbai'  
  13.     }).  
  14.     when('/viewJaipur',  
  15.     {  
  16.         templateUrl: 'jaipur',  
  17.         controller: 'AddJaipur'  
  18.     }).  
  19.     otherwise(  
  20.     {  
  21.         redirectTo: '/viewDelhi'  
  22.     });  
  23. }]);  
Pass Parameter to Route Urls

In previous example we learn how to define the routes for different templates. Now we learn how to pass parameter in Route Urls. In below example we created a list of students, this list contain name, id, city and class information of student. Now we want to create a program through which we can filter the students record as per their city. For this we create a single template and define multiple route for different city. We distinguish the route according the parameter that is passed in route urls.( i.e. href = "#StudentList/0").

Example:
  1. <html>  
  2.   
  3.     <head>  
  4.         <title>Angular JS Views</title>  
  5.         <scriptsrc="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">  
  6.             </script>  
  7.             <scriptsrc="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js">  
  8.                 </script>  
  9.                 <style>  
  10.                 a {  
  11.                     margin-right: 20px;  
  12.                 }  
  13.                  
  14.                 #div1 {  
  15.                     background-color: aqua;  
  16.                     width: 500px;  
  17.                 }  
  18.                  
  19.                 #h1 {  
  20.                     color: red;  
  21.                 }  
  22.                 </style>  
  23.     </head>  
  24.   
  25.       
  26. <body>  
  27.     <h2>AngularJS Routing and View Application</h2>  
  28.     <divng-app="myApp">  
  29.         <ahref="#StudentList/0">Student of All City  
  30.         </a>  
  31.         <ahref="#StudentList/1">Student of Delhi City  
  32.         </a>  
  33.         <ahref="#StudentList/2">Student of Alwar City  
  34.         </a>  
  35.         <ahref="#StudentList/3">Student of Jaipur City  
  36.         </a>  
  37.         <divng-view>  
  38.         </div>  
  39.         <scripttype="text/ng-template" id="Demo.htm">  
  40.             <div id="div1">  
  41.                 <h2 id="h1">{{message}}</h2>  
  42.                 <table>  
  43.                     <th>  
  44.                         <td>Id</td>  
  45.                         <td>Name</td>  
  46.                         <td>Class</td>  
  47.                         <td>City</td>  
  48.                     </th>  
  49.                     <trng-repeat="studnt in Data()">  
  50.                         <td>{{studnt.Id}}</td>  
  51.                         <td>{{studnt.Name}}</td>  
  52.                         <td>{{studnt.Class}}</td>  
  53.                         <td>{{studnt.City_}}</td>  
  54.                     </tr>  
  55.                     <table>  
  56.                     </div>  
  57.                 </script>  
  58.             </div>  
  59.         <script>  
  60.             varmainApp = angular.module("myApp", ['ngRoute']);  
  61.             mainApp.config(['$routeProvider', function ($routeProvider)  
  62.             {  
  63.                 $routeProvider.  
  64.                 when('/StudentList/:id',  
  65.                 {  
  66.                     templateUrl: 'Demo.htm',  
  67.                     controller: 'myController'  
  68.                 }).  
  69.                 otherwise(  
  70.                 {  
  71.                     redirectTo: '/StudentList/0'  
  72.                 });  
  73.             }]);  
  74.             mainApp.controller('myController', function ($scope, $routeParams)  
  75.             {  
  76.                 $scope.data = $routeParams.id;  
  77.                 $scope.studentObject = [  
  78.                 {  
  79.                     Id: 10,  
  80.                     Name: "Pankaj",  
  81.                     Class: 12,  
  82.                     City_: "Alwar"  
  83.                 },  
  84.                 {  
  85.                     Id: 11,  
  86.                     Name: "Sandeep",  
  87.                     Class: 11,  
  88.                     City_: "Jaipur"  
  89.                 },  
  90.                 {  
  91.                     Id: 12,  
  92.                     Name: "Neeraj",  
  93.                     Class: 12,  
  94.                     City_: "Delhi"  
  95.                 },  
  96.                 {  
  97.                     Id: 13,  
  98.                     Name: "Rahul",  
  99.                     Class: 10,  
  100.                     City_: "Alwar"  
  101.                 },  
  102.                 {  
  103.                     Id: 14,  
  104.                     Name: "Sanjeev",  
  105.                     Class: 11,  
  106.                     City_: "Jaipur"  
  107.                 },  
  108.                 {  
  109.                     Id: 15,  
  110.                     Name: "Priya",  
  111.                     Class: 10,  
  112.                     City_: "Delhi"  
  113.                 },  
  114.                 {  
  115.                     Id: 16,  
  116.                     Name: "Narendra",  
  117.                     Class: 12,  
  118.                     City_: "Alwar"  
  119.                 },  
  120.                 {  
  121.                     Id: 17,  
  122.                     Name: "Omveer",  
  123.                     Class: 11,  
  124.                     City_: "Jaipur"  
  125.                 },  
  126.                 {  
  127.                     Id: 18,  
  128.                     Name: "Divyanshu",  
  129.                     Class: 11,  
  130.                     City_: "Delhi"  
  131.                 }];  
  132.                 $scope.Data = function ()  
  133.                 {  
  134.                     varObj = new Array();  
  135.                     if ($scope.data == 0)  
  136.                     {  
  137.                         $scope.message = "List Of All Student";  
  138.                         return $scope.studentObject;  
  139.                     }  
  140.                     elseif($scope.data == 1)  
  141.                     {  
  142.                         $scope.message = "List Of Delhi Student";  
  143.                         for (i = 0; i < $scope.studentObject.length; i++)  
  144.                         {  
  145.                             if ($scope.studentObject[i].City_ === "Delhi")  
  146.                             {  
  147.                                 Obj.push($scope.studentObject[i]);  
  148.                             }  
  149.                         }  
  150.                         returnObj;  
  151.                     }  
  152.                     elseif($scope.data == 2)  
  153.                     {  
  154.                         $scope.message = "List OfAlwar Student";  
  155.                         for (i = 0; i < $scope.studentObject.length; i++)  
  156.                         {  
  157.                             if ($scope.studentObject[i].City_ === "Alwar")  
  158.                             {  
  159.                                 Obj.push($scope.studentObject[i]);  
  160.                             }  
  161.                         }  
  162.                         returnObj;  
  163.                     }  
  164.                     elseif($scope.data == 3)  
  165.                     {  
  166.                         $scope.message = "List Of Jaipur Student";  
  167.                         for (i = 0; i < $scope.studentObject.length; i++)  
  168.                         {  
  169.                             if ($scope.studentObject[i].City_ === "Jaipur")  
  170.                             {  
  171.                                 Obj.push($scope.studentObject[i]);  
  172.                             }  
  173.                         }  
  174.                         returnObj;  
  175.                     }  
  176.                 }  
  177.             });   
  178.         < /script>   
  179.     < /body>   
  180. < /html>  
  181.                             
  182.                                   
Output:

List of all students:

tere sang movie torrent

List of student belong to Alwar City.

List of student belong to Alwar City

In above example we created a single template and define the routing url that also contain a parameter of integer type. According the value of this parameter we are doing the filtration of student record.

I hoped you liked this article, thanks for reading.

Read more articles on AngularJS:


Similar Articles