简体   繁体   English

UI-Router并解析控制器中的未知提供程序

[英]UI-Router and resolve, unknown provider in controller

I am using resolve in my UI-Router .state() call. 我在UI-Router .state()调用中使用resolve。 In my controller I can access the values no problem, but it is throwing an error as follows: 在我的控制器中,我可以访问值没有问题,但它抛出一个错误如下:

$injector/unpr?p0=ctrlOptionsProvider%20%3C-trlOptions $注射器/ unpr?P0 = ctrlOptionsProvider%20%3C-trlOptions

The following code throws the error but allows me to access the variable ctrlOptions just fine: 以下代码抛出错误但允许我访问变量ctrlOptions就好了:

.state('new_user', {
            url: "/user/new",
            templateUrl: "views/user/new.html",
            data: {pageTitle: 'New User'},
            controller: "UserController",
            resolve: {

                ctrlOptions: ['$stateParams', function($stateParams) {

                    return {

                        view: 'new_user',
                    }
                }],
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({ 
                        name: 'MetronicApp',  
                        insertBefore: '#ng_load_plugins_before', // load the above css files before '#ng_load_plugins_before'
                        files: [
                            '../../../assets/global/plugins/bootstrap-datepicker/css/datepicker3.css',
                            '../../../assets/global/plugins/select2/select2.css',

                            '../../../assets/global/plugins/bootstrap-datepicker/js/bootstrap-datepicker.js',
                            '../../../assets/global/plugins/select2/select2.min.js', 
                        ]                    
                    });
                }]
            }
        })

MetronicApp.controller('UserController', ['$rootScope', '$scope', '$http', '$stateParams', 'ctrlOptions', function($rootScope, $scope, $http, $stateParams, ctrlOptions, $timeout) {}

Any idea how to fix this? 知道如何解决这个问题吗?

Thanks 谢谢

Remove ng-controller="UserController" from your view, let the router instantiate the controller. 从视图中删除ng-controller="UserController" ,让路由器实例化控制器。

When you use route resolve argument as dependency injection in the controller bound to the route, you cannot use that controller with ng-controller/or any other directive because the service provider with the name ctrlOptions does not exist. 在绑定到路由的控制器中使用route resolve参数作为依赖项注入时,不能将该控制器与ng-controller /或任何其他指令一起使用,因为名称为ctrlOptions的服务提供程序不存在。 It is a dynamic dependency that is injected by the router when it instantiates the controller to be bound in its respective partial view. 它是一个动态依赖项,由路由器在实例化控制器以在其各自的局部视图中绑定时注入。 It is also a better practice not to instantiate a controller with ng-controller and starting a template with ng-controller directive when router can instantiate and bind the controller to that template, template will be more reusable as it is not tightly coupled with the controller name (with ng-controller="ctrlName" ) but only with the contract. 当路由器可以实例化并将控制器绑定到该模板时,不使用ng-controller实例化控制器并使用ng-controller指令启动模板也是一种更好的做法,模板将更加可重用,因为它与控制器没有紧密耦合name(使用ng-controller =“ctrlName”)但仅限于合同。

So I had the same problem but my setup is different. 所以我有同样的问题,但我的设置是不同的。 Just posting it in case anyone runs into it too. 只是发布它,以防任何人遇到它。 First time posting an answer so not sure if there's a better place to put an answer that doesn't solve the OP's answer but still solves a variant of the question asked. 第一次发布答案,所以不确定是否有更好的地方来回答不能解决OP的答案,但仍然解决了问题的变体。

App State 应用状态

I have a parent state "app" and a child state "home". 我有一个父状态“app”和一个子状态“home”。 When a User logs in they go through the "app" state which did the resolving and then they get redirected to the "home" state. 当用户登录时,他们通过“app”状态进行解析,然后他们被重定向到“home”状态。

angular
.module('common')
.component('app', {
    templateUrl: './app.html',
    controller: 'AppController',
    bindings: {
        member: '=',
        organization: '=',
    }
})
.config(function ($stateProvider) {
    $stateProvider
        .state('app', {
            redirectTo: 'home',
            url: '/app',
            data: {
                requiredAuth: true
            },
            resolve: {
                member: ['AuthService',
                    function (AuthService) {
                        return AuthService.identifyMember()
                            .then(function (res) {
                                AuthService.setAuthentication(true);
                                return res.data;
                            })
                            .catch(function () {
                               return null;
                            });
                    }
                ],
                organization: ['AuthService',
                    function (AuthService) {
                        return AuthService.identifyOrganization()
                            .then(function (res) {
                                return res.data;
                            })
                            .catch(function () {
                                return null;
                            });
                    }
                ],
                authenticated: function ($state, member) {
                    if (!member)
                        $state.go('auth.login');
                }
            },
            component: 'app',
        });
});

The solution for me was in the bindings . 我的解决方案是绑定 I had done it for member before but forgot to do it for organization so that was my silly problem. 我之前已经为会员做了这件事,但忘了为组织做这件事,这是我的愚蠢问题。

Home State 老家

angular
.module('components')
.component('home', {
    templateUrl: './home.html',
    controller: 'HomeController',
    bindings: {
        member: '=',
        organization: '=',
    }
})
.config(function ($stateProvider) {
    $stateProvider
    .state('home', {
        parent: 'app',
        url: '/home',
        data: {
            requiredAuth: true
        },
        component: 'home',
        resolve: {
            'title' : ['$rootScope',
                function ($rootScope) {
                    $rootScope.title = "Home";
                }
            ],
        }
    });
});

Again the solution was in the bindings . 解决方案再次出现在绑定中

NOTE: I did not have to inject the data into my controller 注意:我没有必要将数据注入我的控制器

function HomeController(AuthService, $state) {
    let ctrl = this;
}

Not sure why I didn't have to inject directly into the controller for it to be available as ctrl.member and ctrl.organization . 不知道为什么我不必直接注入控制器,因为它可用作ctrl.memberctrl.organization

I just had this problem and for me problem was that I've assigned controller generally to angular module AND to ui router. 我刚遇到这个问题,对我来说问题是我将控制器一般分配给角度模块和ui路由器。 eg 例如

I had: 我有:

 let app: ng.IModule = angular.module('userManagementApp'); app.controller(PartyViewController.IID, PartyViewController); //NOTICE THIS app.config(['$locationProvider', '$routeProvider', ($locationProvider: ng.ILocationProvider, $routeProvider: any) => { $routeProvider.when("something", { templateUrl: 'views/partyView/partyView.html', controller: PartyViewController.IID, //NOTICE THIS controllerAs: 'pvCtrl', reloadOnSearch: false, resolve: { ... } }) 

But I should have: 但我应该:

 let app: ng.IModule = angular.module('userManagementApp'); app.config(['$locationProvider', '$routeProvider', ($locationProvider: ng.ILocationProvider, $routeProvider: any) => { $routeProvider.when("something", { templateUrl: 'views/partyView/partyView.html', controllerAs: 'pvCtrl', reloadOnSearch: false, resolve: { ... }, controller: PartyViewController }) 

Problem occured because angular tried to load this controller by its own but it shouldn't - router should. 问题发生,因为角度试图自己加载这个控制器但它不应该 - 路由器应该。

Is it potentially because of the extra comma here? 这可能是因为这里有额外的逗号吗?

return {
         view: 'new_user',
        }

Try taking the comma out. 试着把逗号拿出来。

Also, you forgot to inject $timeout to your controller after ctrlOptions 此外,您忘记在ctrlOptions之后向控制器注入$timeout

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

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