$rootScope.$broadcast - confusing argument

Milan13
Giga Expert

Hello,

in my service portal widget I have this client script snippet of code:

$scope.copyFromPreviousTimesheet = function(){

$rootScope.$broadcast('tcp.copy.timesheet');
};

This is triggered by clicking a button - please see below snippet of HTML template:

<button type="button" class="btn" ng-click="copyFromPreviousTimesheet()" aria-haspopup="true">${Copy from previous Time Sheet}</button>

I guess the code in bold (tcp...) should be a script include or something like that but cannot find anything like that in my script include list.

Also what makes me really confused is that probably another widget is generated by clicking the button and I have no idea how this could be done via script include.

Appreciate any help on this.

Milan

5 REPLIES 5

Lansford Hazel
Giga Expert

Hi,

'tcp.copy.timesheet' is is not a script include, it's just the name of an event. In angular, broadcast is used to send an event to other angular directives/widgets.

In order to listen to a broadcast, you must use angulars $on function.

example

$scope.$on('tcp.copy.timesheet', function(){
  //do something
})

 

More information can be found here

 

 

 

Chuck Tomasi
Tera Patron

I agree with LJ. We were just talking about this live on the community live stream video (link coming) when LJ posted this. Thank you.

You can also send arguments from $root.$broadcast with something like this:

$rootScope.$broadcast('eventName', true);

Then your function has something to use as data when the event is picked up in the other widget.

$rootScope.$on('eventName', function(event,data) {
   var myVar = data;
});

Hi Chuck,

In my view we should never use $rootScope.$on but $scope.$on as the earlier would add a new listener every time you navigate back to the page. It would end up in multiple listeners for the same event.

Prasant Kumar 1
Kilo Sage

Hi,

The Code Snippet: $rootScope.$broadcast('tcp.copy.timesheet');  is used to fire an  event with the event name as- tcp.copy.timesheet.

So, Go to Registry module of Events and check with the Name passed as argument.

 

 

Please Mark my answer correct and helpful if it helps you.

Thanks & Regards,

Prasant kumar sahu