加载或导航器时加载顺序不同 [英] Controller load order differs when loading or navigating

查看:87
本文介绍了加载或导航器时加载顺序不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的控制器的结构可能是应该为此负责。在这里,您可以看到<一个href=\"https://github.com/tyrsius/recall/blob/e41f48d5e0afc74a2f4e8e39e89d3600dc7ed8ac/app/coffee/controllers/peopleController.coffee\"相对=nofollow>父和<一个href=\"https://github.com/tyrsius/recall/blob/e41f48d5e0afc74a2f4e8e39e89d3600dc7ed8ac/app/coffee/controllers/childAssociationController.coffee\"相对=nofollow>子控制器,但重要的部分是在这里:

So the structure of my controller may be to blame for this. Here you can see the parent and child controllers, but the important parts are here :

父控制器

//Load version
$scope.person = People.people.get({ id: $routeParams.id}, function(person) {
     var associations = {
         events: person.events || [],
         people: person.people || [],
         places: person.places || []
     };
     $scope.$broadcast('setAssociations', associations);
});
//Event version
$scope.$on('getAssociations', function(e, args) {
  var associations = {
    events: $scope.person.events || [],
    people: $scope.person.people || [],
    places: $scope.person.places || []
  };
  $scope.$broadcast('setAssociations', associations);
});

儿童控制器

$scope.$on('setAssociations', function(event, args) {
    $scope.parentEvents = args.events;
});

$scope.$emit('getAssociations', {});

的想法是,我需要得到来自父母的事件 对象到孩子控制器。问题是,提高对每种方法 setAssociations 从父只在一个方面的工作。

The idea is that I need to get the events from the parent's person object down to the child controller. The problem is, each method of raising setAssociations from the parent only works in one context.

负荷版本的作品。然而,重装或从外部(满载)航行时,孩子事件从未提出,presumably因为它没有注册的听众呢。

The load version works if the page is navigated to from within the app (hash, navigation). However, when reloading or navigating from outside (full load), the child event is never raised, presumably because it hasn't registered its listeners yet.

事件版本的作品,如果页面被刷新,或从外面的应用程序导航到(任何导致网页完全加载)。当导航到,在 $范围是空的,虽然,这种方法失败。

The event version works if the page is refreshed, or navigated to from outside the app (anything causing the page to fully load). When navigated to, the $scope is empty though, and this method fails.

所以,当深层链接,或清爽,当做内部导航中的应用程序加载控制器的顺序是不同的。 我怎样才能从具有父子关系的角度控制器一致加载行为?

So the order in which the app loads the controllers is different when "deep-linking" or refreshing, and when doing internal navigation. How can I get consistent load behavior from angular controllers that have a parent child relationship?

推荐答案

看着从不同的角度的问题,因为孩子示波器中典型的父作用域继承,这意味着我们定义一个父范围的任何属性都立即访问孩子......范围即使您以后添加属性到父范围,在创建子范围之后。换句话说,你可能没有使用事件得到你想要孩子的信息。

Looking at the problem from a different perspective, since child scopes prototypically inherit from parent scopes, this means that any properties we define on a parent scope are immediately accessible in the child scope... even if you add properties to the parent scope later, after the child scope is created. In other words, you might not have to use an event to get the information you want to the child.

如果你的问题很简单,就是孩子范围需要从父范围的一些信息,只是分配数据到一个新的父$ scope属性,让您的小孩视图访问它。当它成为定义,​​它会出现在子视图。取而代之的 VAR协会,试试 $ scope.associations

If your problem is simply that the child scope needs some information from the parent scope, just assign that data to a new parent $scope property, and have your child view access it. When it becomes defined, it will appear in the child view. Instead of var associations, try $scope.associations.

下面是一个使用一个做作小提琴 NG-点击一些数据添加到父作用域,然后变得可见在子视图。

Here is a contrived fiddle that uses an ng-click to add some data to a parent scope, which then becomes visible in the child view.

这是否解决问题了吗?

这篇关于加载或导航器时加载顺序不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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