angularjs - how to broadcast and emit events to other controllers in angular that have no parent-child relationship -
i trying hints post - working $scope.$emit , $scope.$on nothing seems work when controllers in no way related each other.
that -
and in ctrlb this:
$rootscope.$broadcast('userhasloggedin', {})
and in ctrla listen so:
$rootscope.$on('userhasloggedin, function(event, data){});
and no - ctrla never receives broadcasted event unless nest ctrlb div inside ctrla div
any idea?
it tough answer without knowing tried. please see plnkr: http://plnkr.co/edit/hyzworgeypllcmzndco2?p=preview
i created 2 controllers, 1 sends text other:
app.controller('ctrla', function($scope, $rootscope) { $scope.submit = function(){ $rootscope.$broadcast('userhasloggedin', $scope.input); } }); app.controller('ctrlb', function($scope) { $scope.$on('userhasloggedin', function(event, data){ $scope.data = data; }); $scope.data = 'nothing'; });
Comments
Post a Comment