Introduction
You might be familiar with AngularJS Routing, and might have used it as well, but what if that routing needs to be applied on fulfillment of certain conditions? For those cases this article will help you.
To explain the conditional routing I am creatinghave created a sample application.
You might have read my previous article i.e. MVC Routing with Angular Routing. The application which was created in that article is also used here, I am just modifying it to my needs.
Step 1:
Firstly, I created a new View which will be used as a login page.
On this page I created two textboxes and a button, which will help to identify user authenticity. Apart from that I have created a div in which some message is written for non-authentic and non-authorized users, and one more div where some links and a container div is created.
- <!DOCTYPE html>
- <html ng-app="angualarModule">
- <head>
- <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js"></script>
- <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular-route.min.js"></script>
- <script src="~/Scripts/AngularController.js"></script>
- </head>
- <body>
- <form ng-controller="indexController">
- <div>
- <fieldset>
- <legend>Login Demo</legend>
- User Name:
- <input type="text" ng-model="authentic.name" />
- <br />
- Password:
- <input type="password" ng-model="authentic.pwd" />
- <br />
- <br />
- <input type="button" value="Submit" ng-click="CheckUser(authentic)" />
- </fieldset>
- </div>
- <div ng-hide="AuthenticUser()">
- <a href="/#/firstPage">Call First Page</a>
- <br />
- <a href="/#/secondPage">Call Second Page</a>
- <div ng-view></div>
- </div>
- <div ng-hide="CheckNotaValidUser()">
- <h2>You are not an authorized user</h2>
- </div>
- </form>
- </body>
- </html>
Step 2:
After this I worked on the Angular part, I created a new controller, where first of all a click function is created. In that function the models value is coming in "authentic" object.
- var angualarModule = angular.module("angualarModule", ['ngRoute']);
- angualarModule.controller("indexController", function ($scope, $rootScope)
- {
- $scope.CheckUser = function (authentic)
- {
- if (authentic.name == "Anubhav" && authentic.pwd == "Chaudhary")
- {
- $rootScope.validUser = true;
- $rootScope.userType = "Admin";
- $rootScope.NotaValidUser = false;
- }
- else if (authentic.name == "Mohit" && authentic.pwd == "Chaudhary")
- {
- $rootScope.validUser = true;
- $rootScope.userType = "Normal";
- }
- else
- {
- $rootScope.validUser = false;
- $rootScope.NotaValidUser = true;
- }
- };
According to the values entered in textboxes, some decisions are made, and accordingly some values are provided to $rootScope objects, which will help us while creating conditional routing.
You can see that the two divs which are created after the login div shown hide on a conditional basis, and for that, I have put functions in them which will provide them boolean value, and according to those values, these divs will be shown or hide. So, let's create these two functions; these will be created in the same Controller wherethe click function was created.
- $scope.CheckNotaValidUser = function ()
- {
- if ($rootScope.NotaValidUser != undefined)
- {
- if ($rootScope.NotaValidUser == true)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- return true;
- }
- }
- $scope.AuthenticUser = function ()
- {
- if ($rootScope.validUser == true)
- {
- return false;
- }
- else
- {
- return true;
- }
- };
Step 3: Now let's create routing, which will work on the click of hyperlinks available in third div.
- angualarModule.config(function ($routeProvider)
- {
- $routeProvider.
- when('/firstPage',
- {
- templateUrl: 'routedemo/first',
- controller: 'routeDemoFirstController'
- }).
- when('/secondPage',
- {
- templateUrl: 'routedemo/second',
- controller: 'routeDemoSecondController'
- })
- })
- angualarModule.controller("routeDemoFirstController", function ($scope)
- {
- $scope.FirstName = "Anubhav";
- $scope.LastName = "Chaudhary";
- })
- angualarModule.controller("routeDemoSecondController", function ($scope)
- {
- $scope.MobileNumber = "123456";
- $scope.EmailID = "[email protected]";
- })
Step 4:
Now comes the main part, i.e., placing the condition for routing. For this I have used "$locationChangeStart" of Angular. This function will hit as soon as user clicks on any hyperlink or goes to some other page. It will work prior to anything else, so here we can check whether the user is authentic/authorized or not to see some values or to visit some page. If he's not, then we can redirect him to some other page, or we can show him some message, or we can also just let him stay on the current page.
- angualarModule.run(function ($rootScope, $location)
- {
- $rootScope.$on("$locationChangeStart", function (event, next, current)
- {
- if ($rootScope.userType != "Admin")
- {
- $rootScope.NotaValidUser = true;
- $location.path("");
- }
- else {
- $rootScope.NotaValidUser = false;
- }
- if ($rootScope.userType == undefined)
- {
- $rootScope.NotaValidUser = false;
- }
- });
- })
You can see that I have checked whether the user is "Admin" or not; if not, then actions are taken accordingly. I will just show him some message and will not allow him to see any other content.
You can redirect the user to some other page using $location.path(). For example, $location.path("/Dashboard");
Now our sample application is created and it's time to run it.
Output
On running the application two textboxes appear with a submit button.
Firstly, I provided the Admin's user name and password and checked whether everything is working fine or not, you can see that two hyperlinks appeared:
As I click on any of them partial view is shown:
After this I provided some other user name who has limited authorization and again hyperlinks appeared:
But if I click on any of the links then a message is displayed, then:
If any non-existing user tries to enter, then nothing is shown to him: