AngularJS - UI 路由器 - 无法使用解析为我的控制器提供数据
AngularJS - UI Router - Unable to use resolve to provide my controller with data
我正在尝试将带有已加载数据的解析对象注入到我的控制器中,但出现 Unknown Provider
错误:
Unknown provider: configServiceProvider <- configService
这是我的代码:
StateProvider
$stateProvider
.state('index', {
abstract: true,
url: "/index",
templateUrl: "#",
resolve: {
configService: function () {
return {
"helloText": "Welcome in Test Panel"
};
}
}
})
控制器
function MainCtrl($scope, configService) {
$scope.config = configService;
};
angular.module('dot', ['ui.router'])
.config(config)
.controller('MainCtrl', MainCtrl)
片段
function config($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("#");
$stateProvider
.state('index', {
abstract: true,
url: "/index",
templateUrl: "#",
resolve: {
configService: function() {
return {
"helloText": "Welcome in Test Panel"
};
}
}
})
};
function MainCtrl($scope, configService) {
$scope.config = configService;
};
(function() {
angular.module('dot', [
'ui.router', // Routing
])
.config(config)
.run(function($rootScope, $state) {
$rootScope.$state = $state;
})
.controller('MainCtrl', MainCtrl)
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.13/angular-ui-router.min.js"></script>
<div ng-app="dot">
<div ng-controller="MainCtrl as main">
<div ui-view>
</div>
</div>
</div>
好像我的解析对象是在我的控制器加载后定义的...我是 angularJS 的新手,我觉得我肯定遗漏了一些非常明显的东西。
谢谢。
ng-controller
和 UI-Router
状态 resolve
不兼容。这就是为什么您的 "another-world" 'MainCtrl' 无法注入 UI-Router 中定义的 resolve/service 的原因。
不过有个简单的方法,就是把它转换成state:
// brand new root state, providing root (index.html) stuff
// not effecting url or state names
.state('root', {
abstract: true,
template: '<div ui-view=""></div>', // a target for child state
resolve: {
configService: function () { // ready for any state in hierarchy
return {
"helloText": "Welcome in Test Panel"
};
}
},
// brand new line, with 'MainCtrl', which is part of UI-Router now
controller: 'MainCtrl',
})
原来的 root 状态 'index' 现在将被放置在一个真实但抽象的内部,url 不影响状态 - 'root'
// adjusted state
.state('index', { // will be injected into parent template
parent: 'root'
abstract: true,
url: "/index",
templateUrl: ...,
// resolve not needed, already done in root
//resolve: { }
})
调整后index.html
<div ng-app="dot">
<div ui-view="></div> // here will be injected root state, with 'MainCtrl'
//<div ng-controller="MainCtrl as main">
// <div ui-view>
// </div>
//</div>
</div>
也许还要检查 -
我正在尝试将带有已加载数据的解析对象注入到我的控制器中,但出现 Unknown Provider
错误:
Unknown provider: configServiceProvider <- configService
这是我的代码:
StateProvider
$stateProvider
.state('index', {
abstract: true,
url: "/index",
templateUrl: "#",
resolve: {
configService: function () {
return {
"helloText": "Welcome in Test Panel"
};
}
}
})
控制器
function MainCtrl($scope, configService) {
$scope.config = configService;
};
angular.module('dot', ['ui.router'])
.config(config)
.controller('MainCtrl', MainCtrl)
片段
function config($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("#");
$stateProvider
.state('index', {
abstract: true,
url: "/index",
templateUrl: "#",
resolve: {
configService: function() {
return {
"helloText": "Welcome in Test Panel"
};
}
}
})
};
function MainCtrl($scope, configService) {
$scope.config = configService;
};
(function() {
angular.module('dot', [
'ui.router', // Routing
])
.config(config)
.run(function($rootScope, $state) {
$rootScope.$state = $state;
})
.controller('MainCtrl', MainCtrl)
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.13/angular-ui-router.min.js"></script>
<div ng-app="dot">
<div ng-controller="MainCtrl as main">
<div ui-view>
</div>
</div>
</div>
好像我的解析对象是在我的控制器加载后定义的...我是 angularJS 的新手,我觉得我肯定遗漏了一些非常明显的东西。
谢谢。
ng-controller
和 UI-Router
状态 resolve
不兼容。这就是为什么您的 "another-world" 'MainCtrl' 无法注入 UI-Router 中定义的 resolve/service 的原因。
不过有个简单的方法,就是把它转换成state:
// brand new root state, providing root (index.html) stuff
// not effecting url or state names
.state('root', {
abstract: true,
template: '<div ui-view=""></div>', // a target for child state
resolve: {
configService: function () { // ready for any state in hierarchy
return {
"helloText": "Welcome in Test Panel"
};
}
},
// brand new line, with 'MainCtrl', which is part of UI-Router now
controller: 'MainCtrl',
})
原来的 root 状态 'index' 现在将被放置在一个真实但抽象的内部,url 不影响状态 - 'root'
// adjusted state
.state('index', { // will be injected into parent template
parent: 'root'
abstract: true,
url: "/index",
templateUrl: ...,
// resolve not needed, already done in root
//resolve: { }
})
调整后index.html
<div ng-app="dot">
<div ui-view="></div> // here will be injected root state, with 'MainCtrl'
//<div ng-controller="MainCtrl as main">
// <div ui-view>
// </div>
//</div>
</div>
也许还要检查 -