{"id":13285,"date":"2014-05-01T10:09:12","date_gmt":"2014-05-01T04:39:12","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=13285"},"modified":"2017-08-03T16:00:50","modified_gmt":"2017-08-03T10:30:50","slug":"angular-routes","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/angular-routes\/","title":{"rendered":"Angular Routes"},"content":{"rendered":"<p>As we all of know that <a title=\"AngularJS Development Company\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\"><strong>AngularJS<\/strong><\/a> is very popular and powerful front-end framework, which helps us to build rich UI\/UX applications. <strong>AngularJS<\/strong> has many cool things\/features, but today we will concentrate on <strong>routes<\/strong> only and will try to make a small <a href=\"http:\/\/www.tothenew.com\/blog\/optimization-of-angularjs-single-page-applications-for-web-crawlers\/\">single page application<\/a> like <strong>Facebook<\/strong>\/<strong>Gmail<\/strong> with the help of <strong>routes<\/strong>, where we will perform User CRUD operations only.<\/p>\n<p>First of all define <strong>route<\/strong> <strong>mapping<\/strong> for all the different Pages:<\/p>\n<p>[js]<br \/>\n\/*<br \/>\n * Route Mapping: defining route for routeTest module, and as from angular 1.2 version<br \/>\n * ngRoute is now different module, which we have to include explicitly in depended module.<br \/>\n * *\/<br \/>\n\/\/ Defining a module with name routeTest and providing ngRoute as dependency.<br \/>\nangular.module(&#8216;routeTest&#8217;, [&#8216;ngRoute&#8217;])<br \/>\n    .config([&#8216;$routeProvider&#8217;, function ($routeProvider) {<br \/>\n        \/\/ Defining different pages with different URLs<br \/>\n        $routeProvider<br \/>\n            \/\/ If Url is \/user\/list then list.html will render and UserListController will execute.<br \/>\n            .when(&#8216;\/user\/list&#8217;, {templateUrl: &#8216;partials\/list.html&#8217;, controller: UserListController})<br \/>\n            \/\/ If Url is \/user\/create then create.html will render and CreateUserController will execute.<br \/>\n            .when(&#8216;\/user\/create&#8217;, {templateUrl: &#8216;partials\/create.html&#8217;, controller: CreateUserController})<br \/>\n            \/\/ If Url is \/user\/edit\/:id then edit.html will render and EditUserController will execute.<br \/>\n            .when(&#8216;\/user\/edit\/:id&#8217;, {templateUrl: &#8216;partials\/edit.html&#8217;, controller: EditUserController})<br \/>\n            \/\/ If Url is \/user\/show\/:id then show.html will render and ShowUserController will execute.<br \/>\n            .when(&#8216;\/user\/show\/:id&#8217;, {templateUrl: &#8216;partials\/show.html&#8217;, controller: ShowUserController})<br \/>\n            \/\/ TODO Define other URL mapping here.<br \/>\n            \/\/ If no URL matches then otherwise will execute and will redirect to \/user\/list<br \/>\n            .otherwise({redirectTo: &#8216;\/user\/list&#8217;});<br \/>\n    }]);<br \/>\n[\/js]<\/p>\n<p>We have declared <strong>controller<\/strong> and <strong>templateUrl<\/strong> with every URL, <strong>controller<\/strong> will execute and html (which is defined with <strong>templateUrl<\/strong>) will render with the corresponding <strong>URL<\/strong>. And that <strong>controller<\/strong> scope will apply to that corresponding render html. Define all HTMLs in partial directive\/folder and defining <strong>Controllers<\/strong> with dummy as body given below (You can see full working source code from this <a href=\"https:\/\/github.com\/AmitThakkar\/AngularDemo\" target=\"_blank\">link<\/a>):<\/p>\n<p>[js]<br \/>\n\/*<br \/>\n * Master Controller, whose scope will be visible to whole body,<br \/>\n * because we will mark it on body tag.<br \/>\n * *\/<br \/>\nfunction MainController($scope) {<br \/>\n    \/\/ Master Controller<br \/>\n}<\/p>\n<p>\/*<br \/>\n * UserListController, who is responsible for \/user\/list Url<br \/>\n * and its scope will be visible to list.html only.<br \/>\n * *\/<br \/>\nfunction UserListController($scope) {<br \/>\n    \/\/ User List functionality<br \/>\n}<\/p>\n<p>\/*<br \/>\n * CreateUserController, who is responsible for \/user\/list Url<br \/>\n * and its scope will be visible to create.html only.<br \/>\n * *\/<br \/>\nfunction CreateUserController($scope, $location) {<br \/>\n   \/\/ Create User functionality<br \/>\n}<\/p>\n<p>\/*<br \/>\n * ShowUserController, who is responsible for \/user\/list Url<br \/>\n * and its scope will be visible to show.html only.<br \/>\n * *\/<br \/>\nfunction ShowUserController($scope, $routeParams) {<br \/>\n    \/\/ Show user functionality<br \/>\n}<\/p>\n<p>\/*<br \/>\n * EditUserController, who is responsible for \/user\/list Url<br \/>\n * and its scope will be visible to edit.html only.<br \/>\n * *\/<br \/>\nfunction EditUserController($scope, $routeParams, $location) {<br \/>\n    \/\/ Edit User functionality<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>Now define a HTML, which will use all these Code:<\/p>\n<p>[html]<\/p>\n<p>&lt;!&#8211;<br \/>\nmarking ng-app directive to html tag with routeTest value, which says that we are<br \/>\ngoing to use AngularJS on whole html and routeTest module will be apply to HTML.<br \/>\n&#8211;&gt;<\/p>\n<p>    &lt;!&#8211; Require AngularJS &#8211;&gt;<\/p>\n<p>    &lt;!&#8211; Require Angular-Route-JS &#8211;&gt;<\/p>\n<p>    &lt;!&#8211; Require Common Utility UnderscoreJS &#8211;&gt;<\/p>\n<p>    &lt;!&#8211; Require custom routeJS &#8211;&gt;<\/p>\n<p>&lt;!&#8211;<br \/>\nMarking MainController to body tag, so whole body will get scope of Main Controller, All other<br \/>\ncontroller will be child Controller of MainController, so we also call it as Master Controller.<br \/>\n &#8211;&gt;<\/p>\n<p>&lt;!&#8211;<br \/>\nMainController has a scope, which will be accessible to whole html body, because body tag is<br \/>\nmarked with MainController, MainController have a property into its scope with  mainPage,<br \/>\nwhich has a property named as \u201ctitle\u201d, which we are accessing with dot(.) operator on mainPage,<br \/>\nAnd mainPage property is being directly accessed from MainController scope.<br \/>\n&#8211;&gt;&lt;\/pre&gt;<br \/>\n&lt;h2&gt;{{mainPage.title}}&lt;\/h2&gt;<\/p>\n<p>&lt;hr \/&gt;<\/p>\n<p>&lt;div&gt;&lt;a href=&quot;#\/user\/list&quot;&gt;List&lt;\/a&gt; |<br \/>\n&lt;a href=&quot;#\/user\/create&quot;&gt;Create&lt;\/a&gt;&lt;\/div&gt;<\/p>\n<p>&lt;hr \/&gt;<\/p>\n<p>&lt;pre&gt;&lt;!&#8211;<br \/>\nDefining ng-view Directive, where all partial html will load based on Route\/URL Mapping. And<br \/>\nController will execute before partial HTML render. That partial HTML will replace itself<br \/>\nwith inner-HTML of ng-view Directive.<br \/>\n&#8211;&gt;<\/p>\n<p>    Loading&#8230;&lt;\/pre&gt;<\/p>\n<p>&lt;hr \/&gt;<\/p>\n<p>&lt;pre&gt;<br \/>\n[\/html]<\/p>\n<p><strong>NOTE:<\/strong> You can checkout full working source code from this <a href=\"https:\/\/github.com\/AmitThakkar\/AngularDemo\" target=\"_blank\">link<\/a>.<\/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 :\u00a0A \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 :\u00a0Updating 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 :\u00a0Adding 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 :\u00a0Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS :\u00a0Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\">AngularJS :\u00a0Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS :\u00a0Multiple Views, Layout\u00a0Template\u00a0and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS :\u00a0Implementing 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><strong>\u00a0<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we all of know that AngularJS is very popular and powerful front-end framework, which helps us to build rich UI\/UX applications. AngularJS has many cool things\/features, but today we will concentrate on routes only and will try to make a small single page application like Facebook\/Gmail with the help of routes, where we will [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1185],"tags":[3955,955,4697,1415,1417,1418,1416,1388],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13285"}],"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\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=13285"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13285\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=13285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=13285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=13285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}