barteloma
barteloma

Reputation: 6875

How can I communicate controllers in angularjs

I have some geographic data and I want to show them in a map. I used mapping applications but first time developing map and angularjs. I want to show clicked data in map. I have two controller named Data and Map. how can I communicate them.

Or is there another short way?

enter image description here

Upvotes: 1

Views: 69

Answers (1)

Dmitri Mogilevski
Dmitri Mogilevski

Reputation: 471

Here are 2 possibilities:

  1. Create a service/factory on the same module. Inject it into both controllers. Add it to $scope

    $scope.sharedService = myInjectedservice;
    

    of both controllers. Now any changes you make to the properties exposed by the service will be visible to both controllers.

  2. Depending on how you designed the controller hierarchy, either $scope.$emit (event travels up to parent scopes) or $scope.$broadcast (event travels down) an event and add a listener for that event to the receiving controller

    $scope.$on("eventName", function(event, params){
         //event handler code goes here
    });
    

Upvotes: 1

Related Questions