当我在 $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()
。我哪里错了?