遇到Angular.js路线问题

时间:2012-06-21 11:06:21

标签: javascript angularjs

下面是我的app.js和controllers.js文件的样子:

angular.module('RayAuth', ['RayAuth.filters', 'RayAuth.services', 'RayAuth.directives']).
  config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider.
      when('/', {
        templateUrl: 'partials/index.html',
        controller: IndexCtrl
      }).
      when('/users', {
        templateUrl: 'partials/users.html',
        controller: UserCtrl
      }).
      when('/controlpanel', {
        templateUrl: 'partials/controlpanel.html',
        controller: ControlCtrl
      }).
      when('/modules/', {
        templateUrl: 'partials/modules.html',
        controller: ModuleCtrl
      }).
      when('/resources/', {
        templateUrl: 'partials/resources.html',
        controller: ResourceCtrl
      }).
      when('/privileges/', {
        templateUrl: 'partials/privileges.html',
        controller: PrivilegeCtrl
      }).
      when('/roles/', {
        templateUrl: 'partials/roles.html',
        controller: RoleCtrl
      }).
      when('/userroles/', {
        templateUrl: 'partials/userroles.html',
        controller: UserRoleCtrl
      }).
      otherwise({
        redirectTo: '/'
      });
    $locationProvider.html5Mode(true);
  }]);

这是我的controllers.js:

'use strict';

function IndexCtrl() {
    IndexCtrl.$inject = [];
}

function UserCtrl() {
    UserCtrl.$inject = [];
}

function ControlCtrl() {
    ControlCtrl.$inject = [];
}

function ModuleCtrl() {
    ModuleCtrl.$inject = [];
}

function ResourceCtrl() {
    ResourceCtrl.$inject = [];
}

function PrivilegeCtrl() {
    PrivilegeCtrl.$inject = [];
}

function RoleCtrl() {
    RoleCtrl.$inject = [];
}

function UserRoleCtrl() {
    UserRoleCtrl.$inject = [];
}

我已将ng-appng-view添加到我的索引文件中但是当我在浏览器中加载它时,它会重定向到http://localhost/而不是http://localhost/ray-auth/我在做什么错了吗?有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

angular.module('RayAuth', ['RayAuth.filters', 'RayAuth.services', 'RayAuth.directives']).
  config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider.
      when('/', {
        templateUrl: 'partials/index.html',
        controller: IndexCtrl
      }).
      when('/users', {
        templateUrl: 'partials/users.html',
        controller: UserCtrl
      }).
      when('/controlpanel', {
        templateUrl: 'partials/controlpanel.html',
        controller: ControlCtrl
      }).
      when('/modules/', {
        templateUrl: 'partials/modules.html',
        controller: ModuleCtrl
      }).
      when('/resources/', {
        templateUrl: 'partials/resources.html',
        controller: ResourceCtrl
      }).
      when('/privileges/', {
        templateUrl: 'partials/privileges.html',
        controller: PrivilegeCtrl
      }).
      when('/roles/', {
        templateUrl: 'partials/roles.html',
        controller: RoleCtrl
      }).
      when('/userroles/', {
        templateUrl: 'partials/userroles.html',
        controller: UserRoleCtrl
      });
    $locationProvider.html5Mode(true);
  }]);

我删除了其他内容,实际上告诉它在所有其他情况下重定向到'/'。

另请注意,

function PrivilegeCtrl() {

}
PrivilegeCtrl.$inject = [];

如上所示,注射应在控制器外进行。

答案 1 :(得分:0)

也许

<!doctype html>
<html lang="en" ng-app="RayAuth">