$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.