In previous articles, we learned AngularJS project setup, styling with bootstrap, AngularJS structure, data-binding, etc. Please have a look at my previous articles before going through this article.
In this tutorial, we are going to learn how we can navigate from one HTML page to another using Angular Routing. AngularJS provides a nice feature that we can leverage to navigate to another page without reloading the whole page. We can use “ngRoute” module to achieve this.
Step 1
When you have a look in “index.html” from previous article (Part – 3), we have loaded an HTML (sfTemplate.html) by using custom directive.<body ng-controller="sfController" class="container">
- <br/>
- <student-form/>
- </body>
But now, we are removing this.
Step 2
Once “<student-form/>” is removed from “index.html”, we will use “ng-view” directive, and this will be the main View that acts as container for all other HTML Views provided by the route.
We define “ng-view” as: <div ng-view></div>
Step 3
Add a div with “ng-view” and inside it, add a new button.
- <body ng-controller="sfController" class="container">
- <h1>Home Page</h1>
- <div ng-view>
- <input type="button" class="btn btn-primary" value="Add new student"/>
- </div>
- </body>
- </html>
Run the application. You will see the output as,
Step 4
Let’s set up routing. In the “StudentFromsApp.js” file, we can set up routing right after the module is created.
- var studentFormsApp = angular.module('studentFormsApp', ["ngRoute"]);
-
- studentFormsApp.config(function ($routeProvider) {
- $routeProvider
- .when("/home", {
- templateUrl: "app/Home.html",
- controller: "homeController"
- })
- .when("/newStudentForm", {
- templateUrl: "app/StudentForm/sfTemplate.html",
- controller: "sfController"
- })
- .otherwise({
- redirectTo: "/home"
- })
- });
In Angular module, we need to pass “ngRoute” as dependencies. Hence, we have,
- var studentFormsApp = angular.module('studentFormsApp', ["ngRoute"]);
From the above code snippet, you can see the basic routing in AngularJS. We take the object “studentFormsApp” and call the “config” method. We pass “$routeProvider” that helps in routing. And from the “$routeProvider”, we have “when” methods for routing.
In the first “when” method, this is client-side routing and we are routing “/home” and when app routes to this, then it will load “app/Home.html” template which is placed inside app folder. This will have controller “homeController”.
In the second “when” method, we are routing to “/newStudentForm”. In this case, it will load “sfTemplate.html” as templateUrl with controller “sfController”. This “sfTemplate.html” is inside the folder “app/StudentForm/sfTemplate.html”.
Step 5
To use routing, we need to use “ng-route” module. So, let's include “angular-route-min.js” in “index.html” file.
Step 6
Let’s add “Home.html” file that is required while routing in Step 3. Right click on “app” folder and add “Home.html” inside it. And move the button from “index.html” into it.
So, in “Home.html”, we will have,
- <input type="button" class="btn btn-primary" value="Add new student" ng-click="addNewStudent()"/>
We also need “homeController”, so let's create a Controller. Add a “HomeController.js” file inside “app” folder. Include this file in “index.html” too.
In “HomeController.js” file, create Controller with the following code snippet.
- studentFormsApp.controller("homeController",
- function ($scope, $location) {
- $scope.addNewStudent = function () {
- $location.path('/newStudentForm');
- };
- })
In the above code snippet of “HomeController”, we are passing a function with “$scope” and “$location” service. We defined a click function for the button in “Home.html” i.e. “$scope.addNewStudent” which takes $location service and calls path method with ‘/newStudentForm’. This will direct to .when("/newStudentForm", which we have defined in routing.
Step 7
Run the application. You will see the following output in home.html.
When you click on “Add New Student” button, you will be redirected to student form like below.
Following are the complete code snippets.
index.html
- <!DOCTYPE html>
- <html ng-app="studentFormsApp">
- <head>
- <title></title>
- <meta charset="utf-8" />
- <link href="Content/bootstrap.min.css" rel="stylesheet" />
- <script src="Scripts/angular.min.js"></script>
- <script src="Scripts/angular-route.min.js"></script>
- <script src="app/StudentFormsApp.js"></script>
- <script src="app/StudentForm/sfController.js"></script>
- <script src="app/HomeController.js"></script>
- <script src="app/StudentForm/sfDirective.js"></script>
- <script src="app/StudentForm/sfService.js"></script>
- </head>
- <body ng-controller="sfController" class="container">
- <h1>Home Page</h1>
- <div ng-view>
-
- </div>
- </body>
- </html>
StudentFormsApp.js
- var studentFormsApp = angular.module('studentFormsApp', ["ngRoute"]);
-
- studentFormsApp.config(function ($routeProvider) {
- $routeProvider
- .when("/home", {
- templateUrl: "app/Home.html",
- controller: "homeController"
- })
- .when("/newStudentForm", {
- templateUrl: "app/StudentForm/sfTemplate.html",
- controller: "sfController"
- })
- .otherwise({
- redirectTo: "/home"
- })
- });
Home.html
- <input type="button" class="btn btn-primary" value="Add new student" ng-click="addNewStudent()"/>
HomeController.js
- studentFormsApp.controller("homeController",
- function ($scope, $location) {
- $scope.addNewStudent = function () {
- $location.path('/newStudentForm');
- };
- });
Get the complete project from GitHub.
We have learned routing in AngularJS. We will continue to learn more in next articles.
Thanks and Happy Coding!