angularjs $broadcast 未反映视图中的值

angularjs $broadcast not reflecting values on view

当我在 $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>

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

看看这个"How Scope.$broadcast() Interacts With Isolate Scopes In AngularJS" http://www.bennadel.com/blog/2725-how-scope-broadcast-interacts-with-isolate-scopes-in-angularjs.htm

对你有帮助。

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

Your problem is you are broadcasting event before registering listener event. After that you are doing $location.path('/users'), which load user template with usersCtrl.

我认为您应该首先进行重定向,然后使用某些 timeout$rootScope 中广播事件,以便 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);
    }
}]);

Make sure the other listener code should be register before broadcasting the event.(Below listener should be registered before broadcasting).

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

For making it more better solution, you could use resolve of $routeProvider

您可以将 $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


};

}])