0

所以我的控制器的结构可能是罪魁祸首。在这里您可以看到控制器和控制器,但重要的部分在这里:

父控制器

//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', {});

这个想法是我需要events从父person对象到子控制器。问题是,setAssociations从父母那里抚养的每种方法都只适用于一种情况。

load如果页面是从应用程序内导航到的(哈希、导航),则该版本有效。但是,当从外部重新加载或导航(完全加载)时,子事件永远不会引发,大概是因为它还没有注册它的侦听器。

event如果页面被刷新或从应用程序外部导航到(任何导致页面完全加载的任何内容),则该版本有效。导航到时,$scope虽然是空的,但此方法失败。

因此,当“深度链接”或刷新以及进行内部导航时,应用程序加载控制器的顺序是不同的。如何从具有父子关系的角度控制器获得一致的负载行为?

4

1 回答 1

5

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.

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-click 将一些数据添加到父范围,然后在子视图中变得可见。

这能解决你的问题吗?

于 2012-12-12T16:06:16.520 回答