UI-Router并解析控制器中的未知提供程序 [英] UI-Router and resolve, unknown provider in controller
问题描述
我在UI-Router .state()调用中使用resolve。在我的控制器中,我可以访问值没有问题,但它抛出错误如下:
I am using resolve in my UI-Router .state() call. 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
$injector/unpr?p0=ctrlOptionsProvider%20%3C-trlOptions
以下代码抛出错误但允许我访问变量 ctrlOptions
就好了:
The following code throws the error but allows me to access the variable ctrlOptions
just fine:
.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?
谢谢
推荐答案
删除 ng-controller =UserController
从您的视图中,让路由器实例化控制器。
Remove ng-controller="UserController"
from your view, let the router instantiate the controller.
当您使用路由解析参数时作为绑定到路由的控制器中的依赖注入,您不能将该控制器与ng-controller /或任何其他指令一起使用,因为名称 ctrlOptions
的服务提供者不存在。它是一个动态依赖项,由路由器在实例化控制器以在其各自的局部视图中绑定时注入。它也是一个bett当路由器可以实例化并将控制器绑定到该模板时,不练习不用ng-controller实例化控制器并使用ng-controller指令启动模板,模板将更加可重用,因为它与控制器名称没有紧密耦合(使用ng -controller =ctrlName)但仅限于合同。
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. 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.
这篇关于UI-Router并解析控制器中的未知提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!