I have an Angular application with 3 sibling components, they are able to access and update the variable \"data\". They are connected to the router, but the data I want to pass
Angular has the facility to share data between siblings through $emit, $broadcast and $on
in angular’s $scope
and $rootScope
event system.
// SiblingOneCtrl
// SiblingTwoCtrl
app.controller('SiblingOneCtrl1',
function SiblingOneCtrl1 ($rootScope) {
$rootScope.$on('rootScope:emit', function (event, data) {
console.log(data); // 'Emit!'
});
$scope.$on('rootScope:broadcast', function (event, data) {
console.log(data); // 'Broadcast!'
});
$rootScope.$on('rootScope:broadcast', function (event, data) {
console.log(data); // 'Broadcast!'
});
});
app.controller('SiblingOneCtrl2',
function SiblingOneCtrl2($rootScope) {
$rootScope.$emit('rootScope:emit', 'Emit!'); // $rootScope.$on
$rootScope.$broadcast('rootScope:broadcast', 'Broadcast'); // $rootScope.$on && $scope.$on
});
also, you can follow this and this