{"id":9050,"date":"2012-10-12T13:05:21","date_gmt":"2012-10-12T07:35:21","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=9050"},"modified":"2017-08-03T14:04:40","modified_gmt":"2017-08-03T08:34:40","slug":"angularjs-implementing-routes-and-multiple-views","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/","title":{"rendered":"AngularJS: Implementing Routes And Multiple Views"},"content":{"rendered":"<p>We have already seen the concept of routes and multiple views in my <a title=\"Multiple Views, Layout Template and Routing\" href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">previous blog<\/a>. Now let&#8217;s see how to implement it in code.<\/p>\n<p>To implement the concept of routing <a title=\"AngularJs App Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">in AngularJS<\/a>, we need to create modules. Inside a module config, we can define routes.<\/p>\n<p>[javascript]<br \/>\nFile : js\/modules.js<br \/>\nangular.module(&#8216;videoModule&#8217;, []).<br \/>\n  config([&#8216;$routeProvider&#8217;, function($routeProvider) {<br \/>\n  $routeProvider.<br \/>\n\twhen(&#8216;\/videos&#8217;, {templateUrl: &#8216;videoList.html&#8217;,   controller: VideoController}).<br \/>\n\twhen(&#8216;\/videos\/:videoId&#8217;, {templateUrl: &#8216;videoDetail.html&#8217;, controller: VideoDetailController}).<br \/>\n      \totherwise({redirectTo: &#8216;\/videos&#8217;});<br \/>\n}]);<br \/>\n[\/javascript]<\/p>\n<p>Here &#8216;videoModule&#8217; is the name of the module. Inside the config API function, we inject the $routeProvider service, which is used to specify routes.<br \/>\nIn each route, we have three things:<br \/>\n1. URL received (eg. &#8216;\/videos&#8217;)<br \/>\n2. TemplateUrl &#8211; The HTML template to load for the URL received<br \/>\n3. Controller- The controller function that the templateUrl will use<br \/>\nThus, the first route in the module defined in line 5 above states that, when the relative URL &#8216;\/videos&#8217; is received, load the videoDetail.html template into the layout page(main.html) and use the properties and methods defined in the controller function VideoController.<\/p>\n<p>Here is how our new module is used.<\/p>\n<p>[html]<br \/>\nFile : main.html<br \/>\n&lt;!Doctype html&gt;<br \/>\n&lt;html ng-app=&quot;videoModule&quot;&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;script type=&quot;text\/javascript&quot; src=&quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.0.1\/angular.min.js&quot;&gt;&lt;\/script&gt;<br \/>\n&lt;script type=&quot;text\/javascript&quot; src=&quot;js\/modules.js&quot;&gt;&lt;\/script&gt;<br \/>\n&lt;script type=&quot;text\/javascript&quot; src=&quot;js\/controllers.js&quot;&gt;&lt;\/script&gt;<br \/>\n&lt;style type=&quot;text\/css&quot;&gt;<br \/>\n\tli{list-style:none;float:left;padding:20px;}<br \/>\n\ta{text-decoration:none;}<br \/>\n&lt;\/style&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n\t&lt;div ng-view&gt;&lt;\/div&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n[\/html]<\/p>\n<p>We tell the application to use our new module by using ng-app=&#8221;videoModule&#8221;. Then we included the modules.js file and controllers.js(will show later) file by using the script tag. In the\u00a0body, we use ng-view attribute. It allows the appropriate template to be inserted into place when a particular URL is received. The template to be inserted is decided by the routing that we have defined in our module. For example, as per routes defined by &#8216;videoModule&#8217;, if URL &#8216;main.html\/videos&#8217; is received, template videoList.html is rendered inside the div containing the ng-view attribute.<\/p>\n<p>We will also need two controller functions- VideoController and VideoDetailController. It&#8217;s a good idea to put all our controller functions in a separate file.<\/p>\n<p>[javascript]<br \/>\nFile : js\/controllers.js<br \/>\nfunction VideoController($http,$scope){<br \/>\n\t$http.get(&#8216;videoList.json&#8217;).success(function(videoList) {<br \/>\n\t\t$scope.data = videoList;<br \/>\n\t});<br \/>\n}<\/p>\n<p>function VideoDetailController($scope,$routeParams,$http){<br \/>\n\t$http.get(&#8216;video&#8217;+$routeParams.videoId+&#8217;Detail.json&#8217;).success(function(videoDetail) {<br \/>\n\t\t$scope.data = videoDetail;<br \/>\n\t});<br \/>\n}<br \/>\n[\/javascript]<\/p>\n<p>The function VideoController() will fetch the\u00a0list of videos from a json file and assign that list to a local variable. The template videoList.html then uses that local variable to show the list of videos.<\/p>\n<p>Now, we need two templates &#8211;<br \/>\n1. videoList.html &#8211; To show the list of videos. When a\u00a0user clicks on any of the videos, he will be taken to the\u00a0detail page for that video.<br \/>\n2. videoDetail.html &#8211; This will show details of the video<\/p>\n<p>[html]<br \/>\nFile : videoList.html<br \/>\n&lt;div&gt;<br \/>\n\t&lt;ul&gt;<br \/>\n\t\t&lt;li ng-repeat=&quot;datum in data&quot;&gt;<br \/>\n\t\t\t&lt;a href=&quot;#\/videos\/{{datum.id}}&quot;&gt;{{datum.name}}&lt;\/a&gt;<br \/>\n\t\t&lt;\/li&gt;<br \/>\n\t&lt;\/ul&gt;<br \/>\n&lt;\/div&gt;<br \/>\n[\/html]<\/p>\n<p>[html]<br \/>\nFile : videoDetail.html<br \/>\n&lt;img ng-src=&quot;{{data.thumbUrl}}&quot; \/&gt;<br \/>\n&lt;br \/&gt;Name : {{data.name}}<br \/>\n&lt;br \/&gt;Id : {{data.id}}<br \/>\n&lt;br \/&gt;Views : {{data.views}}<br \/>\n&lt;br \/&gt;Comments : {{data.comments}}<br \/>\n[\/html]<\/p>\n<p>When the application starts, the page main.html is loaded. According to route definitions specified in videoModule, it will match the route &#8216;otherwise&#8217; and thus a\u00a0user will be redirected to &#8216;main.html#\/videos&#8217;. The corresponding template videoList.html will be loaded into main.html. Post this a user will see a list of videos. When a\u00a0user clicks on a video, for example, video with id 2, the resulting URL will be &#8216;main.html#\/videos\/2&#8217;. This URL corresponds to route &#8216;\/videos\/:videoId&#8217;, where the value of videoId=2.<\/p>\n<p>Thus the template &#8216;videoDetail.html&#8217; is loaded into the main.html. The value of videoId can be retrieved from route &#8216;\/videos\/:videoId&#8217; by using the $routeParams.videoId as shown in the videoDetailController function. This function simply fetches data of the selected video from its json file and assigns it to a local variable, which is then used by the videoDetail.html template to show details of video. For example, if videoId=2, then the json file from which data will be fetched is video2Detail.json.<\/p>\n<p>That was just a small example illustrating concept of routes and multiple views in AngularJS. I will be back with more.<\/p>\n<p><span style=\"font-size: 20px; color: blue;\">Read Further on AngularJS Series<\/span><\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS : A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS : Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS : Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS : Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS : Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\"> AngularJS : Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS : Multiple Views, Layout Template and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS : Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have already seen the concept of routes and multiple views in my previous blog. Now let&#8217;s see how to implement it in code. To implement the concept of routing in AngularJS, we need to create modules. Inside a module config, we can define routes. [javascript] File : js\/modules.js angular.module(&#8216;videoModule&#8217;, []). config([&#8216;$routeProvider&#8217;, function($routeProvider) { $routeProvider. [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1439,3429],"tags":[3180,3955,955,4697,1852,1101,1100],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9050"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=9050"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9050\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=9050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=9050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=9050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}