简体   繁体   English

不能在UI路由器AngularJS的视图中使用其他控制器

[英]cannot use a different controller with a view with ui-router AngularJS

I'm using ui-router for routing my angular app. 我正在使用ui-router路由我的角度应用程序。 I have the following route: 我有以下路线:

'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'
        });
    }
]);

This is within my 'Admin' module, and that has its own controller (AdminsController). 这是在我的“管理员”模块中,并且具有自己的控制器(AdminsController)。

However, I want to be able to create a new user from here, and the controller to do that is called 'AuthenticationController' and is elsewhere in the structure. 但是,我希望能够从这里创建一个新用户,并且要执行此操作的控制器称为“ AuthenticationController”,并且在结构中的其他位置。

As far as I know, I should be able to put something like: 据我所知,我应该可以这样写:

state('adduser', {
                url: '/admin/create',
                templateUrl: 'modules/admins/views/create-admin.client.view.html',
                controller:'AuthenticationController'

            })

and it should work. 它应该工作。

However - I keep getting redirected back to the index page. 但是-我一直被重定向回索引页面。

I suspect this may be because I'm using HTML5 and have this in my config also: 我怀疑这可能是因为我正在使用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'

Can anyone help? 有人可以帮忙吗?

You should add : 您应该添加:

$urlRouterProvider.otherwise("/home");

And declare your controller inside your state function (not in your view files) like that : 然后在状态函数内声明控制器(不在视图文件中),如下所示:

 state('viewAdmin', {
        url: '/admins/:adminId',
        controller : AdminsController,
        templateUrl: 'modules/admins/views/view-admin.client.view.html'
 })

If it doesn't work, try to remove : 如果它不起作用,请尝试删除:

 $locationProvider.html5Mode(true).hashPrefix('!');
            //Sets the HTTP header type for the redirects
 $httpProvider.defaults.headers.common = {
     'Accept': 'application/json', 
     'Content-Type': 'application/json'

Let me know ;) 让我知道 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM