路由解析+多控制器组合? [英] Route resolving + multiple controller combo?

查看:17
本文介绍了路由解析+多控制器组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 2 个概念结合起来,但在让它们协同工作时遇到了麻烦.

Concept 1:路由解析.这很容易解释,我只是想像这样解析某些模型:

$routeProvider.when("/新闻", {templateUrl: "news.html",控制器: "newsCtrl",解决:{ 新闻:函数(模型){ 返回 Model.news.getList();}

Concept 1:我可以在路由 controller: xyz 中使用的基本控制器"的想法,然后在实际视图 html 中加载子' 控制器.例如....

app.js

$routeProvider.when("/新闻/最新消息", {templateUrl: "news-latest.html",控制器:newsCtrl"

news-latest.html

...

这将允许我将通用代码放在 newsCtrl 中,并将 /news/latest 特定代码放在 newsLatestCtrl

问题是我需要结合这两个概念,但很难做到,因为我无法将局部变量传递给子"控制器,只能传递给主控制器.

我看到的唯一选项看起来并不是什么好主意...

  1. 在基本控制器中,将局部变量添加为 $scope 变量(看起来很脏,尤其是对于大型控制器)
  2. 将其移动到服务中(尽管它与任何服务都没有真正相关...)
  3. ??

解决方案

当我们不知道基本控制器"的职责是什么时,很难为您提供帮助.对我来说,你要找的Concept 3如下:

$routeProvider.when("/新闻", {templateUrl: "news.html",控制器: "newsCtrl",解决:{ 新闻:函数(模型){ 返回 Model.news.getList();}).when("/新闻/最新消息", {templateUrl: "news.html",控制器:newsLatestCtrl"},解决:{ newsLatest: function(Model) { return Model.news.getLatest();});module.controller('newsCtrl', function($scope, news) {/* 想要分享的常用代码 */});module.controller('newsLatestCtrl', function($scope, newsLatest, $controller) {//实例化你的基本控制器"var ctrl = $controller('newsCtrl', {"$scope": $scope,新闻":新闻最新});//为最新消息自定义并添加特殊代码});

I'm trying to combine 2 concepts and I'm having trouble making them work together.

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: 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>

This will allow me to put my common code inside newsCtrl, and /news/latest specific code in newsLatestCtrl

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...

  1. In the base controller, add the local variable as a $scope variable (seems dirty, especially for large controllers)
  2. Move it into a service (it's not really relevant to any services though...)
  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆