Skip to main content

Angular JS Routing Step by Step using Visual Studio.



In this post, we will see step by step how to create an Angular JS Project using Visual Studio. This is for beginners only J

Before we start, make sure
·        You have very basic understanding of AngularJS
·        Basic understanding of Routing
·        Beginner level knowledge on html5
·        Hands-On using Visual Studio

1.      Open Visual Studio and click “new project”.
Select “ASP.NET Web Application”.
Enter Project Name, Location and Solution Name.


2.  Create an “Empty” ASP.NET Web Application as shown in below figure


3.  Open Solution Explorer, right click on the solution and click “Manage Nuget Packages”

Install Angular.js
Once installed, you will see all Angular.JS references in “Scripts” folder.

4.  In Solution Explorer, create a folder “views” and add three html pages as shown below:
Below is the code for Index.html:

<!DOCTYPE html>
<html ng-app="app">
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
</ul>
</nav>
<div>
<ng-view>
</ng-view>
</div>
<script src="../Scripts/Vendor/angular.min.js"></script>
<script src="../Scripts/Custom/App.js"></script>
</body>
</html>

Before the end of closure of body tag, add the script references to angular.js and app.js as highlighted in yellow.

Few Best Practices to be followed in the .html file
  • Placing script tags at the end of the page improves app load time because the HTML loading is not blocked by loading of the angular.js script. 
  • ng-app should be placed at the root of your application, typically on the <html> tag so that Angular JS will auto-bootstrap our application.
  • Consider adding an ng-strict-di directive on the same element as ng-app to ensure that all services in your application are properly annotated.

5.   Below is the code for Home.html and About.html pages
Note: Both these html files have the same code, but the controllers will be different and we will see the controller related code in the next steps.

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<div>
{{title}
</div>
</body>
</html>


6.   Within the “Scripts” folder, I have created another folder named “Custom”.
This is for all our custom code/js files.
I have created another folder “Vendor” and moved all “Angular JS” related files to “Vendor”.

Add a js file and name it as “App.JS” under the “Custom” folder.



Below is the code in “App.js” file.

var app = angular.module("app", []);
app.controller("HomeController", function ($scope) {
   $scope.title = "THIS IS HOME PAGE"
});
app.controller("AboutController", function ($scope) {
    $scope.title = "THIS IS ABOUT PAGE"
});

We have the basic structure ready. Now, let’s start adding the logic for “Routing”.

1.  First step is to add Angular Route Script Reference in Index.html in the order as shown in below snapshot
2.    Now, we need to add or inject the dependency for Angular Route in our App.js file.
var app = angular.module("app", ["ngRoute"]);

3.    Next step is to configure Routing
   For this, let us add a config section in our App.JS file.

$routeProvider.when("/home", {
templateUrl: "home.html",
controller: "HomeController"
   })
.when("/about", {
templateUrl: "about.html",
controller: "AboutController"
       })
.otherwise({redirectTo: "/home"});
});

Below is a snapshot of the complete code in App.js file.



4.  The last step is to update the “Routes” in our Index.html.

Press F5 to run the application and you will see the navigation items and routing behaviour.

This is how we have implemented simple Angular JS application with Routing!
Hope this helps! 

Comments

  1. I gone through your blog, It was very much usefull for me,I gained so much of information through your blog ,This was very helfull and keep posting more,Thank you.

    Best Angularjs Training in Chennai

    Angular 6 Training in Chennai

    Angular 5 Training in Chennai

    ReplyDelete
  2. I have gone through your blog, it was very much useful for me and because of your blog, and also I gained many unknown information, the way you have clearly explained is really fantastic. Kindly post more like this, Thank You.
    Aviation Academy in Chennai
    Aviation Courses in Chennai
    best aviation academy in chennai
    aviation institute in chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

How to get lookup value(username) from people picker column using JavaScript(Client Object Model)?

Recently I faced an issue on fetching username from people picker column. Below is the code which can be used in such scenarios- // JavaScript source code function getUsers() { //Get the current client context ctx = new SP.ClientContext(webUrl); // Get Web web = ctx.get_web(); // Get List list = web.get_lists().getByTitle("My List"); camlQry = new SP.CamlQuery(); //Traverse through all files and folders deep and get some people picker fields like "Author" camlQry.set_viewXml('<View Scope=\'RecursiveAll\'>' + '<Query>' + '<ViewFields>' + '<FieldRef Name=\'' + colValuesEnum.Title + '\' />' + '<FieldRef Name=\'' + colValuesEnum.Author + '\' />' + '</ViewFields> ' + '</View>'); listItem = list.getItems(camlQry); ctx.load(listItem); ctx.executeQueryAsync(success, failed); } //Success CallBack Function fun

SharePoint Script to update MMD Column in Library

This post contains the code to update Managed Metadata column value in Library. Add-PSSnapin Microsoft.Sharepoint.Powershell cls #Log Variables $basePath = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent Set-Location $basePath #SP variables $web=$null ; $list=$null; $SiteURL = "https://mysharepointsite/en-us"# Library Site Url $ListName = "Pages";# List Name $FieldName="Document" $termStoreName = "Managed Metadata Service Application" $termGroupName = "Document_Content" $termSetName = "DocumentName" $MMDValueToUpdate = "Technical Doc" #Pre-requisite checks Try {     #Web Check     $web = Get-SPWeb $SiteURL     if($web -eq $null)     {       Write-Host "Cannot find an SPSite object that contains the following Id or Url: $SiteURL"  $_.Exception.Message        Exit     }     #List Check     $list = $web.Lists[$ListName]     if($list -eq $null)     {        Write-H