[英]How to inject a Controller into state object inside of Config? (ui-router)
Below I'm trying to inject the PlatformCtrl into my $stateProvider object: 下面,我试图将PlatformCtrl注入我的$ stateProvider对象中:
"use strict";
module.exports = angular.module('tickertags', [
'templateCache',
'headers', // headers (platform, view, timespan)
'tickertags-auth', // authentication / authentication_module.js
'settings', // settings
'tags', // tags / tags_module.js
'tickers', // tickers / tickers_module.js
'ui.mask', // https://github.com/angular-ui/ui-mask
'ui.router', // https://github.com/angular-ui/ui-router
'ui.bootstrap' // https://github.com/angular-ui/bootstrap,
])
.config([
'$stateProvider',
'$urlRouterProvider',
'PlatformCtrl',
function(
$stateProvider,
$urlRouterProvider,
PlatformCtrl) {
//...
const platformHeader = {
name: 'platformheader',
templateUrl: "headers/platform/platform_header.html",
controller: PlatformCtrl,
controllerAs: "ph",
transclude: true,
bindings: {
ticker: '<',
startEpoch: '<',
endEpoch: '<',
timespan: '<',
feed_open: '<'
}
};
$stateProvider
.state(login)
.state(password)
.state(passwordReset)
.state(settings)
.state(settingsDefault)
.state(settingsAlerts)
.state(platformHeader);
Getting the following error: 出现以下错误:
Failed to instantiate module tickertags due to: Error: [$injector:unpr] Unknown provider: PlatformCtrl 由于以下原因,无法实例化模块标签:错误:[$ injector:unpr]未知提供程序:PlatformCtrl
module.exports = angular
.module('headers')
.controller('PlatformCtrl', PlatformCtrl);
PlatformCtrl.$inject = [
'api',
'$element'];
function PlatformCtrl(
api,
$element) {
//....
What I'm trying to avoid is having to place all my Controller code for each Component in my app.js > config space: 我要避免的是必须将每个组件的所有控制器代码都放在app.js>配置空间中:
$stateProvider.state('tags', {
parent: 'tickers',
url: "/:ticker",
views: {
'tagsPanel@': {
template: '<p>Tags for {{$ctrl.ticker}}</p>',
controllerAs: '$ctrl',
bindToController: true,
controller: function($stateParams) {
this.$onInit = function() {
console.info('Tags Panel 2 $onInit');
this.ticker = $stateParams["ticker"];
}
}
}
}
});
^ my app.js would be huge and the Controllers should be modularized anyways. ^我的app.js很大,无论如何,控制器都应该模块化。
The first thing is that controllers are not available during config phase. 第一件事是控制器在配置阶段不可用。 The second thing is that controllers aren't available for DI, they are instantiated with $controller
service. 第二件事是控制器不可用于DI,它们通过$controller
服务实例化。
It is 它是
controller: 'PlatformCtrl',
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.