我正在使用 ui-router 来路由我的 Angular 应用程序。我有以下路线:
'use strict';
//Setting up route
angular.module('admins').config(['$stateProvider',
function($stateProvider) {
// Admins state routing
$stateProvider.
state('admin_home', {
url: '/admin',
templateUrl: 'modules/admins/views/list-admins.client.view.html'
}).
state('adduser', {
url: '/admin/create',
templateUrl: 'modules/admins/views/create-admin.client.view.html',
}).
state('viewAdmin', {
url: '/admins/:adminId',
templateUrl: 'modules/admins/views/view-admin.client.view.html'
}).
state('editAdmin', {
url: '/admins/:adminId/edit',
templateUrl: 'modules/admins/views/edit-admin.client.view.html'
});
}
]);
这是在我的“管理”模块中,并且有自己的 Controller (AdminsController)。
但是,我希望能够从这里创建一个新用户,并且执行此操作的 Controller 称为“AuthenticationController”,位于结构中的其他位置。
据我所知,我应该能够输入如下内容:
state('adduser', {
url: '/admin/create',
templateUrl: 'modules/admins/views/create-admin.client.view.html',
controller:'AuthenticationController'
})
它应该可以工作。
但是 - 我不断被重定向回索引页面。
我怀疑这可能是因为我使用的是 HTML5 并且我的配置中也有这个:
// use HTML 5
$locationProvider.html5Mode(true).hashPrefix('!');
//Sets the HTTP header type for the redirects
$httpProvider.defaults.headers.common = {
'Accept': 'application/json',
'Content-Type': 'application/json'
有人可以帮忙吗?
最佳答案
您应该添加:
$urlRouterProvider.otherwise("/home");
并在状态函数中(而不是在 View 文件中)声明您的 Controller ,如下所示:
state('viewAdmin', {
url: '/admins/:adminId',
controller : AdminsController,
templateUrl: 'modules/admins/views/view-admin.client.view.html'
})
如果不起作用,请尝试删除:
$locationProvider.html5Mode(true).hashPrefix('!');
//Sets the HTTP header type for the redirects
$httpProvider.defaults.headers.common = {
'Accept': 'application/json',
'Content-Type': 'application/json'
让我知道;)
关于javascript - 无法使用带有 ui-router AngularJS View 的不同 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30385214/