Michael
Michael

Reputation: 13616

How can I pass between controllers in same module?

In module named EventDetails I have two controllers attachedFilesList and EventDetailsctrl.

I need to pass string value from EventDetailsctrl controller to attachedFilesList controller.

How can I pass between controllers in same module?

Upvotes: 0

Views: 40

Answers (2)

Arun AK
Arun AK

Reputation: 4370

You can also pass the data from one controller to another controller using service.

EventDetails
  .controller('attachedFilesList', ['$rootScope', '$scope', 'myservice',
     function($rootScope, $scope, myservice) {
         $scope.myservice = myservice;
     }
  ]);

EventDetails
  .controller('EventDetailsctrl', ['$rootScope', '$scope', 'myservice',
     function($rootScope, $scope, myservice) {
         $scope.myservice = myservice;
     }
  ]);

EventDetails
   .service('myservice', function() {
       this.name = "value";
   });

Here is the Plnkr

Hope it helps :)

Upvotes: 1

Suneet Bansal
Suneet Bansal

Reputation: 2702

Solution is attached below:

In EventDetailsctrl

$rootScope.$broadcast('pass-value', 'dummyVal');

In attachedFilesList

$scope.$on('pass-value', function(event, value) {
  // value is the object which is passed from $broadcast
});

Upvotes: 3

Related Questions