【问题标题】:How start route in controller Angular JS?如何在控制器 Angular JS 中启动路由?
【发布时间】:2015-04-08 11:37:58
【问题描述】:

我需要在控制器 ChatController 中调用该代码,而不是在全局应用程序中:

.config(function($routeProvider){
            $routeProvider.when("/chat/dialog/:id",
                {
                    templateUrl: "/template/chat/active_dialog.html",
                    controller: "ChatController"
                }
            );
        })

我该怎么做?

我试过了(div中没有​​加载模板):

Angular JS:

$scope.selectDialog = function (id, event){
   $scope.template = '/template/chat/active_dialog.html';
});

HTML:

<div ng-include src="{{template}}"></div>

【问题讨论】:

  • 看起来与stackoverflow.com/questions/16384134/… 类似。这是您要找的吗?
  • 你能说得更具体一点吗? “不在全球应用中”是什么意思?是否要在控制器中定义 Route(不可能)?
  • 是的,我想在Controller中定义路由,不可以吗?那我怎么加载模板呢?
  • $routeProvider 只能在应用程序的配置阶段访问,这意味着您不能在控制器中使用$routeProvider。模板是从你在templateUrl中设置的url自动加载的
  • 您可以为此创建一个单独的模块吗?我想您需要为模块之间共享的任何内容创建第三个模块。

标签: angularjs


【解决方案1】:

我同意@Nano,所有使用 angular 的提供程序都是 注入 并用于 .config,你不能直接在你的控制器中使用它。

【讨论】:

  • 你是在调用这个函数$scope.selectDialog()吗?
猜你喜欢
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2015-07-25
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多