角度 $broadcast 不起作用. [英] Angular $broadcast not working.
本文介绍了角度 $broadcast 不起作用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 $scope.$on在两个控制器之间共享信息a> 和 $scope.$broadcast.
这是视图:
<div ng-controller="ChildCtrl">{{内容}}
和控制器:
.controller('ParentCtrl', ['$scope', function($scope) {$scope.$broadcast('someEvent', 'bidule');}]).controller('ChildCtrl', ['$scope', function($scope) {$scope.$on('someEvent', function(event, b) {$scope.content = b;});}])
和 plunker
我在这里做错了什么?
解决方案
子控制器尚未实例化.将 broadcast
包装在 timeout
中可以工作:http://plnkr.co/edit/MF7P16K1OTv46JNgkkih?p=preview
$timeout(function(){$scope.$broadcast('someEvent', 'bidule');});
I am trying to share informations between two controllers with $scope.$on and $scope.$broadcast.
Here is the view:
<div ng-controller="ParentCtrl">
<div ng-controller="ChildCtrl">
{{content}}
</div>
</div>
and the controllers:
.controller('ParentCtrl', ['$scope', function($scope) {
$scope.$broadcast('someEvent', 'bidule');
}])
.controller('ChildCtrl', ['$scope', function($scope) {
$scope.$on('someEvent', function(event, b) {
$scope.content = b;
});
}])
and the plunker
What am I doing wrong here?
解决方案
The child controller isn't instantiated yet. Wrapping the broadcast
in a timeout
works:
http://plnkr.co/edit/MF7P16K1OTv46JNgkkih?p=preview
$timeout(function(){
$scope.$broadcast('someEvent', 'bidule');
});
这篇关于角度 $broadcast 不起作用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文