Reputation: 3601
Example :
<div ng-controller="ParentCtrl as parent" class="ng-scope">
{ { parent.data } }
<div ng-controller="SiblingOneCtrl as sib1" class="ng-scope">
{ { sib1.data } }
</div>
</div>
<script>
function ParentCtrl ($scope) {
$scope.$broadcast('parent', 'Some data'); // идет вниз!
}
function SiblingOneCtrl ($scope) {
$scope.$on('parent', function (event, data) {
console.log(data); // ‘Some data’
});
}
</script>
Download a sample on this forum and people write it working
opening the console, I have not seen any effects Waiting tips ..thank you indifferent people
Upvotes: 1
Views: 1270
Reputation: 23231
foxx told right Try it with with $timeout in parentctrl as :
function ParentCtrl ($scope,$timeout) {
this.data="r";
$timeout(function(){
$scope.$broadcast('parent', 'Some data'); // идет вниз!
},1000)
}
function SiblingOneCtrl ($scope) {
$scope.$on('parent', function (event, data) {
console.log(data); // ‘Some data’
});
}
see plunker for working code
Upvotes: 4
Reputation: 4908
Happens most likely because it fires the broadcast before that listener in sibling is even created.
a) don't broadcast immediately, using delay($timeout
) for example..
b) or pass the data some other way, using prototype inheritance or even a service(you set in on parent and get it in sibling)
c) you most likely want to broadcast something on ng-click or something anyway, don't you? in which case it should work just fine as it is.
Upvotes: 4