$ routeParams是主控制器空 [英] $routeParams is empty in main controller

查看:102
本文介绍了$ routeParams是主控制器空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这片布局的HTML:

I have this piece of layout html:

<body ng-controller="MainController">
  <div id="terminal"></div>

  <div ng-view></div>

  <!-- including scripts -->
</body>

现在显然,当我尝试使用 $ routeParams MainController ,它总是空的。需要注意的是 MainController 应该是效力于每一个可能的途径是非常重要的;所以,我是不是在我的app.js.定义它我的意思是,我不是在这里定义它:

Now apparently, when I try to use $routeParams in MainController, it's always empty. It's important to note that MainController is supposed to be in effect in every possible route; therefore I'm not defining it in my app.js. I mean, I'm not defining it here:

$routeProvider.when("/view1", {
  templateUrl: "partials/partial1.html"
  controller: "MyCtrl1"
})

$routeProvider.when("/view2", {
  templateUrl: "partials/partial2.html"
  controller: "MyCtrl2"
})

// I'm not defining MainController here!!

其实,我觉得我的问题是完全一样的这一个: HTTPS ://groups.google.com/forum/#​​话题/角/ ib2wHQozeNE

不过,我还是不明白如何让我的主控制器路由参数...

However, I still don't get how to get route parameters in my main controller...

编辑:

我的意思是,我不是我MainController与任何特定路由关联。它的定义;和它的所有其他控制器的父控制器。我想要知道的是,当你去到一个URL如 /不管,这是由像 /路由匹配:无论,这是为什么认为只有副控制能够访问路线参数,而主控制器没有?我如何获得:无论路由参数在我的主控制器

What I meant was that I'm not associating my MainController with any specific route. It's defined; and it's the parent controller of all other controllers. What I'm trying to know is that when you go to a URL like /whatever, which is matched by a route like /:whatever, why is it that only the sub-controller is able to access the route parameter, whereas the main controller is not? How do I get the :whatever route parameter in my main controller?

推荐答案

$ routeParams 服务异步填充。这意味着,它通常会出现空当在一个控制器首先使用。

The $routeParams service is populated asynchronously. This means it will typically appear empty when first used in a controller.

已被填充 $ routeParams 时得到通知,订阅的<$ c中的 $ routeChangeSuccess 事件$ C> $范围。 (如果你在不访问一个孩子 $范围 A成分是,例如,一个服务或一个工厂,你可以注入,并使用 $ rootScope 来代替。)

To be notified when $routeParams has been populated, subscribe to the $routeChangeSuccess event on the $scope. (If you're in a component that doesn't have access to a child $scope, e.g., a service or a factory, you can inject and use $rootScope instead.)

module.controller('FooCtrl', function($scope, $routeParams) {
  $scope.$on('$routeChangeSuccess', function() {
    // $routeParams should be populated here
  });
);

通过路由使用的控制器,或者通过路由包含在模板中,必须将完全填充的 $ routeParams ,因为纳克立即访问-view 等待继续之前的 $ routeChangeSuccess 事件。 (它的的等待,因为它需要以决定哪个模板/控制器,甚至负载的路由信息​​。)

Controllers used by a route, or within a template included by a route, will have immediate access to the fully-populated $routeParams because ng-view waits for the $routeChangeSuccess event before continuing. (It has to wait, since it needs the route information in order to decide which template/controller to even load.)

如果你知道你的控制器的内部使用 NG-视图,你将不再需要等待路由事件。如果你知道你的控制器不会,你会的。如果你不知道,你必须明确允许这两种可能性。订阅 $ routeChangeSuccess 是不够的;你只会看到事件,如果 $ routeParams 是不是已经填充:

If you know your controller will be used inside of ng-view, you won't need to wait for the routing event. If you know your controller will not, you will. If you're not sure, you'll have to explicitly allow for both possibilities. Subscribing to $routeChangeSuccess will not be enough; you will only see the event if $routeParams wasn't already populated:

module.controller('FooCtrl', function($scope, $routeParams) {
  // $routeParams will already be populated
  // here if this controller is used within ng-view

  $scope.$on('$routeChangeSuccess', function() {
    // $routeParams will be populated here if
    // this controller is used outside ng-view
  });
);

这篇关于$ routeParams是主控制器空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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