We have already heard a lot about AngularJS and SPA (Single Page Application). But when we start thinking to develop an SPA application, we have a lot of confusion ie: how to develop SAP application, what steps should I take to develop SAP etc. So here I am going to explain step by step development of a SPA (Single Page Application).

So for SPA we have ng-route and ng-view directive. Ng-route manage the routing of a web application and ng-view allows us to render different view depending on the router.

Now open Visual Studio



Figure 1: New Project

Now we need to add AngularJS reference into our application. So right click on Solution Explorer, then select Manage Nuget Packages...



Figure 2: Manage Nuget



Figure 3: AngularJS



Figure 4: AngularJS Installed

Now again right click on Project’s Solution Explorer, then Add New Item.



Figure 5: Add New Item



Figure 6: Item Name

Now add a JavaScript file in your project, so right click on project’s Solution Explorer and Add New Item.



Figure 7: Add JavaScript



Figure 8: MyScript

Here's your MyScript.js file with the following code:

  1. var app = angular.module('single-page-app', ['ngRoute']);
  2. app.config(function ($routeProvider) {
  3. $routeProvider
  4. .when('/', {
  5. templateUrl: 'Home.html'
  6. })
  7. .when('/Article', {
  8. templateUrl: 'Article.html'
  9. })
  10. .when('/Blog', {
  11. templateUrl: 'Blog.html'
  12. })
  13. .when('/ContactUs', {
  14. templateUrl: 'ContactUs.html'
  15. })
  16. .when('/AboutUs', {
  17. templateUrl: 'AboutUs.html'
  18. });
  19. });
  20. app.controller('cfgController', function ($scope) {
  21. /*
  22. Here you can handle controller for specific route as well.
  23. */
  24. });


Figure 9: Code Image

Now Add Home.html, Article.html, Blog.html, ContactUs.html & AboutUs.html in your application.

Home.html

  1. <div style="padding-left:130px;padding-right:200px;">
  2. <h1> Welcome RS - To the SPA World! - HOME</h1>
  3. <p>Develop SPA (Single Page Application Step By Step.)</p>
  4. </div>


Figure 10: Html

Article.html



Figure 11: Article.html

Blog.html



Figure 12: Blog

AboutUs.html



Figure 13:
AboutUs

ContactUs.html



Figure 14: ContactUs

Now open your Index.html and here's the code:
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Single page web app using Angularjs</title>
  6. <script src="Scripts/angular.min.js"></script>
  7. <script src="Scripts/angular-route.min.js"></script>
  8. <script src="Scripts/MyScript.js"></script>
  9. <style type="text/css">
  10. #navcontainer ul
  11. {
  12. margin: 0;
  13. padding: 0;
  14. list-style-type: none;
  15. text-align: left;
  16. padding-top: 4px;
  17. }
  18. #navcontainer ul li
  19. {
  20. display: inline;
  21. }
  22. #navcontainer ul li a
  23. {
  24. text-decoration: none;
  25. padding: .2em 1em;
  26. color: white;
  27. background-color: red;
  28. }
  29. #navcontainer ul li a:hover {
  30. color: #fff;
  31. background-color: #369;
  32. }
  33. </style>
  34. </head>
  35. <body ng-app="single-page-app">
  36. <div ng-controller="cfgController">
  37. <div>
  38. <table style="width:100%; background-color:skyblue;">
  39. <tr><td style="background-color:blue; color:white; font-size:30pt; text-align:center; padding:10px; height:50px;">SPA - (Single Page Application - AngularJS)</td></tr>
  40. <tr>
  41. <td id="navcontainer">
  42. <nav>
  43. <ul>
  44. <li><a href="#/">Home</a></li>
  45. <li><a href="#/Article">Articles</a></li>
  46. <li><a href="#/Blog">Blog</a></li>
  47. <li><a href="#/ContactUs">Cotact us</a></li>
  48. <li><a href="#/AboutUs">About us</a></li>
  49. </ul>
  50. </nav>
  51. </td>
  52. </tr>
  53. <tr>
  54. <td style="height:20pt;"></td>
  55. </tr>
  56. <tr>
  57. <td>
  58. <div ng-view>
  59. <!--
  60. This section Will show SPA concept.
  61. -->
  62. </div>
  63. </td>
  64. </tr>
  65. <tr>
  66. <td style="height:20pt;"></td>
  67. </tr>
  68. <tr>
  69. <td>@Copyright : RS</td>
  70. </tr>
  71. <tr>
  72. <td style="height:4pt;"></td>
  73. </tr>
  74. </table>
  75. </div>
  76. </div>
  77. </body>
  78. </html>


Figure 15:
Defoult

Now run you application:



Figure 16: Run

Click on Articles Menu and see your URL and content.



Figure 17: Article Menu



Figure 18: Blog Menu



Figure 19: Contact Menu



Figure 20: About Menu