Passing value from one widget to another in Service Portal

ashwanikumar
Tera Expert

Hi All,

I have written following client Controller scripts in two widgets to pass value which is not working:

1st instance to pass value

 

function($scope, spUtil,  $rootScope) {
	/* widget controller */
	var c = this;
var selectedName = ''; //value which needs to be passed
setTimeout(function () {
			var obj= {
			value : selectedName
		};
		$rootScope.$broadcast('cent', obj);
		alert("Selected value is: " + selectedName+" object value "+obj.value);
		
},1000);
}

2nd instance which receives the value

function($scope , $rootScope) {
var c = this;
		$rootScope.$on('cent', function(event,obj) {
		 c.aLocation = obj.value;
		 alert("From Admin Option: " + c.aLocation);
		 c.server.update();
		   });
}

Where am doing wrong?

(is there any reference where I can read and understand the concept?)

Thanks

Kumar

4 REPLIES 4

Brad Tilton
ServiceNow Employee
ServiceNow Employee

What exactly are you trying to do and when you say it's not working, what is actually happening? Is it throwing JS errors, not doing anything?

Here is a good reference and tutorial that includes passing an event from one widget to another: https://developer.servicenow.com/app.do#!/training/article/app_store_learnv2_serviceportal_kingston_...

nathanfirth
Tera Guru

I wrote a tutorial a while back about using Angular.js events to communication between widgets.

Hopefully, this is helpful: https://serviceportal.io/using-events-communicate-widgets/

Thanks,

-----------------
Nathan Firth
Founder and ServiceNow Architect
NewRocket, Inc.
nathan.firth@newrocket.com
http://serviceportal.io
http://newrocket.com

Jon Barnes
Kilo Sage

In my opinion you are better off using angular services to do this instead of using angular event model. Angular services are meant for this exact purpose (sharing data between controllers). I have a pretty simple example of using services on my share app here. The app itself has nothing to do with your use case, but it contains an example of using services to share data with controllers:

https://developer.servicenow.com/app.do#!/share/contents/9271804_service_portal_background_process_w...

You can read more about angular services here. It will be worth your time to learn to use these:

https://docs.angularjs.org/guide/services

 

leo48
Tera Contributor

Hey, check the youtube vidoe i made for pass data between 2 widgets https://www.youtube.com/watch?v=LooT596ZhtQ