{"id":978,"date":"2014-12-11T22:24:21","date_gmt":"2014-12-11T16:54:21","guid":{"rendered":"http:\/\/codeforgeek.com\/?p=978"},"modified":"2021-06-20T06:06:38","modified_gmt":"2021-06-20T00:36:38","slug":"single-page-web-app-angularjs","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/single-page-web-app-angularjs\/","title":{"rendered":"Single page web app using AngularJs"},"content":{"rendered":"<p>AngularJs provides <strong>ng-route<\/strong> and <strong>ng-view<\/strong> directive and by using them we can easily develop our single page web app. In this tutorial, I am going to show you how to develop a simple one-page web application using AngularJs.<\/p>\n<p><strong>ng-route<\/strong> manage <a href=\"https:\/\/codeforgeek.com\/express-complete-tutorial-part-3\/\" title=\"Express Complete Tutorial : Part 3\" target=\"_blank\" rel=\"noopener noreferrer\">routing<\/a> of web application. By routing i mean when you click on link in ordinary web page you get redirected to target anchor link. By using ng-route we can handle those routes.<br \/>\n<center><a class=\"button-rounded button-flat-caution\" href=\"http:\/\/demo.codeforgeek.com\/single-page-app-angularjs\/\" title=\"Single page app angularjs\" target=\"_blank\" rel=\"noopener noreferrer\"><i class=\"fa fa-code\"><\/i>LIVE DEMO<\/a> <a class=\"button-rounded button-flat\" href=\"https:\/\/codeload.github.com\/codeforgeek\/single-page-app-angularjs\/zip\/master\" target=\"_blank\" rel=\"noopener noreferrer\"><i class=\"fa fa-github\"><\/i>DOWNLOAD<\/a><\/center><\/p>\n<p><strong>ng-view<\/strong> allow us to render different templates on particular <a href=\"https:\/\/codeforgeek.com\/express-complete-tutorial-part-2\/\" title=\"Express Complete Tutorial : Part 2\" target=\"_blank\" rel=\"noopener noreferrer\">view<\/a> depending upon the router. For example, if user hits <strong>example.com\/about<\/strong> then route will tell to load particular template for this route. <\/p>\n<h3>Our project:<\/h3>\n<p>We are building simple web app, we are consist of following files.<br \/>\n<code><br \/>\n---- index.html ( Starting page)<br \/>\n---- home.html  ( template )<br \/>\n---- about.html ( template )<br \/>\n---- script.js  ( Angular code )<br \/>\n<\/code><br \/>\nHere is how it works.<br \/>\n<center><a href=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/12\/working.png\"><img decoding=\"async\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/12\/working-300x230.png\" alt=\"working\" width=\"300\" height=\"230\" class=\"alignnone size-medium wp-image-995\" style=\"border: 2px solid grey;\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/12\/working-300x230.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/12\/working-768x589.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/12\/working.png 969w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/center><br \/>\nBefore explanation let me show you the code. First have a look at index.html.<\/p>\n<div class=\"file_name\"><strong>index.html<\/strong><\/div>\n<p><code lang=\"html\"><br \/>\n<!doctype html><br \/>\n<html lang=\"en\"><br \/>\n<head><br \/>\n  <meta charset=\"UTF-8\"><br \/>\n  <title>Single page web app using Angularjs<\/title><br \/>\n  <script src=\"\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.3.3\/angular.min.js\"><\/script><br \/>\n  <script src=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/angular.js\/1.3.3\/angular-route.min.js\">   <\/script><br \/>\n<script src=\"script.js\"><\/script><br \/>\n<\/head><br \/>\n<body ng-app=\"single-page-app\"><\/p>\n<div ng-controller=\"cfgController\">\n<div>\n<nav>\n<ul>\n<li><a href=\"#\/\">Home<\/a><\/li>\n<li><a href=\"#\/about\">About us<\/a><\/li>\n<\/ul>\n<\/nav>\n<\/div>\n<p><\/p>\n<div ng-view>\n  <!--\n      This DIV loads templates depending upon route.\n  -->\n  <\/div>\n<\/p><\/div>\n<p><\/body><br \/>\n<\/html><br \/>\n<\/code><br \/>\nHere is our <a href=\"https:\/\/codeforgeek.com\/angularjs\/\" target=\"_blank\" rel=\"noopener noreferrer\">angular<\/a> code. Responsible for handling routes and updating view.<\/p>\n<div class=\"file_name\"><strong>script.js<\/strong><\/div>\n<p><code lang=\"javascript\"><br \/>\nvar app=angular.module('single-page-app',['ngRoute']);<br \/>\napp.config(function($routeProvider){<br \/>\n      $routeProvider<br \/>\n          .when('\/',{<br \/>\n                templateUrl: 'home.html'<br \/>\n          })<br \/>\n          .when('\/about',{<br \/>\n                templateUrl: 'about.html'<br \/>\n          });<br \/>\n});<br \/>\napp.controller('cfgController',function($scope){<\/p>\n<p>    \/*<br \/>\n\tHere you can handle controller for a specific route as well.<br \/>\n    *\/<br \/>\n});<br \/>\n<\/code><br \/>\nIf you have noticed <strong>$routeProvide<\/strong> here we are loading home and about HTML files into view. Let&#8217;s have a look on to them too.<\/p>\n<div class=\"file_name\"><strong>home.html<\/strong><\/div>\n<p><code lang=\"html\"><\/p>\n<h1>Hello, world!<\/h1>\n<p>....<\/p>\n<p>\n    This is executed because of this code.<\/p>\n<pre>\r\n      <code>\r\n        .when('\/',{\r\n              templateURl: 'home.html'\r\n        })\r\n      <\/code>\r\n    <\/pre>\n<\/p>\n<p><\/code><\/p>\n<div class=\"file_name\"><strong>about.html<\/strong><\/div>\n<p><code lang=\"html\"><\/p>\n<h1>About us page.<\/h1>\n<p>......<\/p>\n<p>\n   This is executed because of this code.<\/p>\n<pre>\r\n     <code>\r\n       .when('\/about',{\r\n             templateURl: 'about.html'\r\n       })\r\n     <\/code>\r\n   <\/pre>\n<\/p>\n<p><\/code><\/p>\n<h3>Explanation:<\/h3>\n<p>The main code which does the magic is this.<br \/>\n<code lang=\"javascript\"><br \/>\n   .when('\/',{<br \/>\n                templateUrl: 'home.html'<br \/>\n          })<br \/>\n          .when('\/about',{<br \/>\n                templateUrl: 'about.html'<br \/>\n          });<br \/>\n<\/code><br \/>\nDepending upon your templates and routes you can add more in it. As you see, when particular URL hits, we simply passing our file URL and Angular automatically Loads it into <strong>ng-view<\/strong>. <\/p>\n<h4>How to run the code:<\/h4>\n<p>Download file, serve it using any web server, for ease you can use <strong>XAMPP<\/strong> package to install web <a href=\"https:\/\/codeforgeek.com\/visualize-server-traffic-logstalgia\/\" title=\"Visualize Server traffic using logstalgia\" target=\"_blank\" rel=\"noopener noreferrer\">server<\/a>. Put directory into HTDOCS folder and run through browser.<\/p>\n<h3>Conclusion:<\/h3>\n<p>Single page web application is very intuitive and easy to use. In this, web app used to load once and then only particular part of app keeps on changing without refreshing whole page which saves bandwidth as well as no loading of external files again and again such as Images or CSS files which in most of web app is static.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AngularJs provides ng-route and ng-view directive and by using them we can easily develop our single page web app. In this tutorial, I am going to show you how to develop a simple one-page web application using AngularJs. ng-route manage routing of web application. By routing i mean when you click on link in ordinary [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1000,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[2,18],"tags":[],"class_list":["post-978","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angularjs","category-tutorial"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/12\/banner32.png",858,288,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/12\/banner32-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/12\/banner32-300x101.png",300,101,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/12\/banner32-768x258.png",768,258,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/12\/banner32.png",858,288,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/12\/banner32.png",858,288,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/12\/banner32.png",858,288,false]},"uagb_author_info":{"display_name":"Shahid","author_link":"https:\/\/codeforgeek.com\/author\/shahid\/"},"uagb_comment_info":0,"uagb_excerpt":"AngularJs provides ng-route and ng-view directive and by using them we can easily develop our single page web app. In this tutorial, I am going to show you how to develop a simple one-page web application using AngularJs. ng-route manage routing of web application. By routing i mean when you click on link in ordinary&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=978"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/978\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/1000"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}