2

i want to send parameters using Angualr broadcast

From one of my controller i do

$rootScope.$broadcast('handlemodeldelete', Idx); // I broadcast value 6

and in another controller i have listener

 $scope.$on('handlemodeldelete', function(Idx)
 {

  });

But Idx value reaches like this after i see from debug window.

currentScope: ChildScope
defaultPrevented: false
name: "handlemodeldelete"
preventDefault: ()
targetScope: Scope
__proto__: Object

Any clue.?

Saurabh Kumar
  • 16,353
  • 49
  • 133
  • 212

2 Answers2

1

The first parameter to your listener function is an event object, then any arguments used in the call to $broadcast come next. Define your handler as follows:

$scope.$on('handlemodeldelete', function(event, Idx)
        {

        });

See the AngularJS docs for more info:

https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$on

bobwienholt
  • 17,420
  • 3
  • 40
  • 48
  • thanks. This solved the problem. But i have the same code else and there it works. I am wondering why ? – Saurabh Kumar Nov 18 '15 at 16:24
  • Not sure. Is it the same version of AngularJS? It is possible that the interface changed? Post the other code so I can take a look. – bobwienholt Nov 18 '15 at 16:28
  • Its same version of angularJS since it is same project and exactly same code. Just the handler name different.Strange . But thanks a lot.:) – Saurabh Kumar Nov 18 '15 at 16:30
1

First param is the event. You need to add another parameter:

$scope.$on('handlemodeldelete', function(e, Idx){
    console.log("Event", e);
    console.log("IDX", Idx);
});

An example for passing multiple params would be:

$scope.$broadcast('eventName', { message: msg,  message2: msg2  });


$scope.$on('eventName', function (event, args) {
     console.log(args.message);
     console.log(args.message2);
});

More info at Angular Docs: https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$broadcast

Roy M J
  • 6,926
  • 7
  • 51
  • 78