-
How to Communicate Between Two Controllers in Angular?
over 8 years ago
-
over 8 years ago
Thanks guys for response, I have made my logic using $rootscope.$broadcast and $scope.$on.
I have called service of $http call and at response I passed my data like $rootScope.$broadcast('resData', response.data); and receive it in other controllers by:$scope.$on('resData', function (event, arg) { $scope.changedData = arg; });
-
over 8 years ago
If you don't mind making the call again, simply placing the function within the service, and injecting the service to all controllers that would call this function seems like it would get you going in the right direction. Once you solve that problem, you can investigate caching the call and somehow providing the data across without making a call to the service every time.
-
over 8 years ago
Please do search on $boardcast , $emit, $on and services that will you an idea about sharing the data
-
-
over 8 years ago
$rootscope is creating scope global to all controllers, is there any other way I can access variable or function of one controller to other controller?
-
-
over 8 years ago
One way I have handled something like this is to attach the data to $rootScope (see http://stackoverflow.com/questions/18880737/how-do-i-use-rootscope-in-angular-to-store-variables for a good explanation on this).
Additionally, once you get this working, I would move your http call into a service, and inject the service in your controller that would be setting the retrieved data to $rootScope, as the http concern belongs in a service, and the controller's responsibility, for the most part, is making data available for the templates by getting that data from a service.
-
5 Answer(s)