-1

I am new to AngularJS and I am having problems trying to push an object from one controller to an array in another controller. Can anyone help me with that? I am looking into Angular $broadcast and $emit but I'm not sure on how to approach it.

AT82
  • 71,416
  • 24
  • 140
  • 167
  • 3
    Possible duplicate of [How to call a function from another controller in angularjs?](http://stackoverflow.com/questions/29467339/how-to-call-a-function-from-another-controller-in-angularjs) – ganesh deshmukh May 03 '17 at 18:07

1 Answers1

0

You can use services for sharing data between controllers. It is better than sending messages. Btw to use $broadcast and $emit you can do the following.

Inside the controller which broadcast the event:

$scope.broadcastEvent = function() {
    $scope.$broadcast('my-broadcast', myElement);
};

Inside the other controller:

$scope.$on('my-broadcast', function(event, data) {
    console.log(data);
});

To decide between $broadcast or $emit you have to see your structure of controllers. Otherwise instead of sending the $broadcast from $scope, you can start the broadcast from $rootScope, and it will be detected everytime.

But again, you do use services for sharing data in these cases

quirimmo
  • 9,800
  • 3
  • 30
  • 45