Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
145
rated 0 times [  152] [ 7]  / answers: 1 / hits: 16995  / 10 Years ago, tue, november 4, 2014, 12:00:00

This may be the repeated question but the workaround I found for this issue is not working in my case thats why I am posting the question.



I've following service:



appRoot.service('MyService', function($rootScope) {
var Messenger = {
Temp: ,
TempId:,
tempMethod: function(Id) {
TempId = Id;
$rootScope.$broadcast('FirstCtrlMethod');
}
};
return Messenger ;
});


In second controller:



appRoot.controller('SecondCtrl', function ($scope, $location, MyResource, NotificationService, MyService) {
$scope.invokeFirstCtrl= function() {
var Id = '2';
MyService.tempMethod(Id);
});


In first controller:



appRoot.controller('FirstCtrl', function ($scope, $compile, $filter, $modal, $sce, $location, NotificationService, MyService) {
$scope.$on('FirstCtrlMethod', function () {
alert('I am from frist controller');
});
});


Problem: The line $rootScope.$broadcast('FirstCtrlMethod'); is executing as expected but it is not causing to fire event $scope.$on('FirstCtrlMethod', function () {.. in the first controller.
I've used the differenct services in many places in my app in the same way and they are workig fine, I am not understanding why it is not working here.


More From » angularjs

 Answers
17

putting comment as an answer...



I guess the other controller which is supposed to receive the event is not yet instatiated when you are $broadcasting the event.



Please try instantiating the other controller


[#68923] Friday, October 31, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jalyn

Total Points: 173
Total Questions: 96
Total Answers: 90

Location: Somalia
Member since Mon, Feb 27, 2023
1 Year ago
;