javascriptangularjsangularjs-ng-route

AngularJS doesn't render view specified in $routeProvider


var myApp = angular.module("myApp", ["ngRoute"]);

myApp.config(function ($locationProvider) {
    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });
});

myApp.config(function ($routeProvider) {
    $routeProvider
        .when("", {
            templateUrl: "Views/Employee/Employees.html",
            controller: "EmployeesController"
        }).otherwise({ redirectTo: 'Views/Employee/Employees' });
});

It routes the URL mentioned in otherwise, not the URL mentioned in the .when


Solution

  • Root route of your Angular app will be "/", not "".

    Then, redirectTo takes as value a route defined in your route provider.

    myApp.config(function ($routeProvider) {
        $routeProvider
            .when("/", {
                templateUrl: "Views/Employee/Employees.html",
                controller: "EmployeesController"
            })
            .otherwise({ redirectTo: '/' });
    });