Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Communicate Between Two Controllers in Angular?

    • 0
    • 0
    • 0
    • 5
    • 0
    • 0
    • 0
    • 446
    Answer it

    I have two controllers in Angular, one controller makes an Http call gets data once the data is received I want all the controllers to invokes a call to update their model or refresh view.

    Any help would be much appreciated.

 5 Answer(s)

  • 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;
     });
    
  • 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.

  • 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.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: