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
的服务提供者会不存在.它是一个动态依赖项,由路由器在实例化要绑定到其各自局部视图中的控制器时注入.当路由器可以实例化控制器并将控制器绑定到该模板时,不要使用 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屋!