gpt4 book ai didi

javascript - 路由解析+多 Controller 组合?

转载 作者:行者123 更新时间:2023-12-02 16:37:00 25 4
gpt4 key购买 nike

我正在尝试结合两个概念,但无法让它们一起工作。

概念1:路由解析。这很容易解释,我只想解决这样的某些模型:

$routeProvider
.when("/news", {
templateUrl: "news.html",
controller: "newsCtrl",
resolve: { news: function(Model) { return Model.news.getList(); }

概念 1:我可以在路由 controller: xyz 中使用“基本 Controller ”的想法,然后在实际的 View html 中加载“sub” ' Controller 。例如......

app.js

$routeProvider
.when("/news/latest", {
templateUrl: "news-latest.html",
controller: "newsCtrl"

news-latest.html

<div ng-controller="newsLatestCtrl">
...
</div>

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

问题是我需要结合这两个概念,但很难做到这一点,因为我无法将局部变量传递到“子” Controller ,只能传递到主 Controller 。

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

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

最佳答案

当我们不知道“基地 Controller ”的职责是什么时,很难为您提供帮助。对我来说,您正在寻找的 Concept 3 如下:

$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
});

关于javascript - 路由解析+多 Controller 组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27849332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com