top button
Flag Notify
Site Registration

Explain $routeProvider in AngularJS?

+1 vote
195 views
Explain $routeProvider in AngularJS?
posted Sep 7, 2017 by Jdk

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

$routeProvider is the key service which set the configuration of URLs, map them with the corresponding HTML page or ng-template, and attach a controller with the same.

Let’s see the following example:

var mainApp = angular.module("mainApp", ['ngRoute']);
mainApp.config(['$routeProvider', function($routeProvider) {
   $routeProvider.
   when('/addEmployee', {
      templateUrl: 'addEmployee.htm', controller: 'AddEmployeeController'
   }).
 otherwise({
      redirectTo: '/addEmployee'
   });
}]);

Following are the important points to be considered in above example.

routeProvider is defined as a function under config of the mainApp module using a key as ‘$routeProvider’.

$routeProvider.when defines a URL “/addEmployee” which is then mapped to “addEmployee.htm”. This <addEmployee.htm> should be present in the same path as main HTML page.
“otherwise” is used to set the default view.
“controller” is used to set the corresponding controller for the view.

answer Oct 30, 2017 by Manikandan J
...