路由解析+多控制器组合? [英] Route resolving + multiple controller combo?
问题描述
我正在尝试将2个概念组合在一起,但在使它们一起工作时遇到了麻烦.
I'm trying to combine 2 concepts and I'm having trouble making them work together.
Concept 1
:路由解析.这很容易解释,我只想解决以下某些模型:
Concept 1
: Route resolving. This is easy to explain, I just want to resolve certain models like this:
$routeProvider
.when("/news", {
templateUrl: "news.html",
controller: "newsCtrl",
resolve: { news: function(Model) { return Model.news.getList(); }
Concept 1
:我可以在路由controller: xyz
中使用的基本控制器"的概念,然后在实际的html中加载"sub"控制器.例如....
Concept 1
: The idea of a 'base controller' that I can use in the route controller: xyz
, then in the actual view html I load the 'sub' controller. For instance....
app.js
$routeProvider
.when("/news/latest", {
templateUrl: "news-latest.html",
controller: "newsCtrl"
news-latest.html
<div ng-controller="newsLatestCtrl">
...
</div>
这将使我可以将通用代码放入newsCtrl
中,并将/news/latest
特定代码放入newsLatestCtrl
This will allow me to put my common code inside newsCtrl
, and /news/latest
specific code in newsLatestCtrl
问题是我需要将这两个概念结合起来,但是很难做到,因为我不能将局部变量传递给"sub"控制器,而只能传递给主控制器.
The problem is I need to combine these two concepts, but it's hard to do that because I can't pass the local variables in to the 'sub' controller, only the main controller.
我看到的唯一选择看起来并不像是好主意...
The only options I see don't really look like good ideas...
- 在基本控制器中,将本地变量添加为
$scope
变量(似乎很脏,尤其是对于大型控制器) - 将其移至服务中(尽管它与任何服务都没有真正的关联...)
- ??
- In the base controller, add the local variable as a
$scope
variable (seems dirty, especially for large controllers) - Move it into a service (it's not really relevant to any services though...)
- ??
推荐答案
当我们不知道基本控制器"的职责是什么时,很难为您提供帮助.对我来说,您正在寻找的Concept 3如下:
It's hard to help you when we don't know what the "base controller"'s responsibilities are. For me, the Concept 3 you are looking for is as follows:
$routeProvider
.when("/news", {
templateUrl: "news.html",
controller: "newsCtrl",
resolve: { news: function(Model) { return Model.news.getList(); })
.when("/news/latest", {
templateUrl: "news.html",
controller: "newsLatestCtrl"},
resolve: { newsLatest: function(Model) { return Model.news.getLatest(); });
module.controller('newsCtrl', function($scope, news) {
/* common code you want to share */
});
module.controller('newsLatestCtrl', function($scope, newsLatest, $controller) {
// instantiate your "base controller"
var ctrl = $controller('newsCtrl', {
"$scope": $scope,
"news": newsLatest
});
// customize and add special code for latest news
});
这篇关于路由解析+多控制器组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!