1

当我在 $rootScope 中添加一个 $broadcast 并将其捕获到其他控制器时,它没有反映任何要查看的值。例子:

// on first ctrl.
controller('firstCtrl', ['$scope','$location', '$rootScope', function($scope, $location, $rootScope) {
    $scope.myFunc = function() { 
        $rootScope.$broadcast('someEvent');
        $location.path('/users');
    }
}])


// On Users Page.
.controller('usersCtrl', ['$scope','$rootScope', function($scope, $rootScope) { 
    $scope.dataFilter = {};
    $rootScope.$on('someEvent', function(event, b) {
        $scope.callOnBroadcast();
    });
  
    // call this function on broadcast.  
    $scope.callOnBroadcast = function() {
      $scope.dataFilter = {
        types: [
          {key: 'tags',     val: 'Collections'},
          {key: 'prices',   val: 'Price'},
          {key: 'ratings',  val: 'Rating'},
          {key: 'designers',val: 'Designer'}
        ],
        data: {tags: [], prices: [], ratings: [], designers: []}

      };

      $scope.$apply();


    };
}])
<h4 data-ng-repeat="ftr in dataFilter.types">  
  {{ftr.val}}
</h4>

当我在上使用MyFunc功能时firstCtrl,它会将我重定向到用户页面,同时广播功能运行。在用户页面上,我$scope.callOnBroadcast()在广播打开时使用,但即使我使用它也反映了视图页面上的任何更改$scope.$apply()。我哪里错了?

4

3 回答 3

2

您的两个控制器都没有任何层次结构,这就是为什么您需要在$rootScope而不是范围内 $broadcast 事件,

您的问题是您在注册侦听器事件之前广播事件。之后你正在做$location.path('/users'),它加载用户模板usersCtrl

我认为您应该首先进行重定向,然后$rootScope以某种方式广播该事件,timeout以使usersCtrl

代码

// on first ctrl.
controller('firstCtrl', ['$scope','$location', '$rootScope',  '$timeout',function($scope, $location, $rootScope, $timeout) {
    $scope.myFunc = function() { 
        $location.path('/users');
        $timeout(function(){
          //boardcast will available to every listener
          $rootScope.$broadcast('someEvent'); 
        },1000);
    }
}]);

确保在广播事件之前应注册其他侦听器代码。(以下侦听器应在广播之前注册)。

$scope.$on('someEvent', function(event, b) {
    $scope.callOnBroadcast();
});

为了使其成为更好的解决方案,您可以使用 resolve of $routeProvider

于 2015-04-09T05:47:17.560 回答
0

您可以将 $rootScope 注入您的控制器并在该级别上广播。您可以在这里看到 $broadcast 生效:http: //plnkr.co/edit/ySrEU4accdgeY7iJhdZi ?p=preview

app.controller('firstCtrl', ['$scope', '$rootScope','$location', function($scope, $rootScope, $location) {
$scope.myFunc = function() { 
    $rootScope.$broadcast('someEvent');
    $location.path('/users');
};

}])


// On Users Page.
.controller('usersCtrl', ['$scope', '$rootScope', function($scope, $rootScope) { 
$scope.dataFilter;
$rootScope.$on('someEvent', function(event, b) {
    $scope.callOnBroadcast();
    //$scope.dataFilter = 'test';
});

// call this function on broadcast.  
$scope.callOnBroadcast = function() {
  $scope.dataFilter = {
    types: [
      {key: 'tags',     val: 'Collections'},
      {key: 'prices',   val: 'Price'},
      {key: 'ratings',  val: 'Rating'},
      {key: 'designers',val: 'Designer'}
    ],
    data: {tags: [], prices: [], ratings: [], designers: []}

  };

  //$scope.$apply();  <- I don't think is necessary


};

}])

于 2015-04-09T05:59:52.130 回答
0

看看这个“Scope.$broadcast() 如何与 AngularJS 中的隔离范围交互” http://www.bennadel.com/blog/2725-how-scope-broadcast-interacts-with-isolate-scopes-in- angularjs.htm

这将对您有所帮助。

于 2015-04-09T05:27:53.840 回答