Global function call for each widget in Service Portal

Deepika Mishra
Mega Guru

Hi All,

Scenario:
I have few widgets where in each widget it does multiplication of two numbers. So in each widget I call a function where it does the multiplication part. These individual widgets have some other functionality as well along with this multiplication.

So, I want to understand is there any way I can have this  "multiplication of two numbers" function in a place (like a global function out of all widgets) defined and call that particular part when required.

Note: I need different widgets which have a common function for multiplication. So, is there any way to only have the function define at a separate place and call when required inside widget.

1 ACCEPTED SOLUTION

Deepika Mishra
Mega Guru

I figured out a way to resolve this issue:

Step 1: Go To Widget Angular Provider

Type: Service

Name: testService

Client Script:

function testService() {
	
   
    this.showResult = function() {
        return "Hi";
    };
}

 

Step 2: Save

Step 3: Open widget where this Angular Provider needs to be called.

Step 4: Go to the Related List of the widget and add the service under "Angular Provider"

Step 5: Add below code in Client Controller of that Widget

api.controller = function($scope,testService) {
    /* widget controller */
    var c = this;
	
	$scope.serviceCall = testService.showResult();
	console.log("$scope.serviceCall: "+$scope.serviceCall);
};

View solution in original post

13 REPLIES 13

Thameem Ansari
Giga Guru
Giga Guru

there is 2 way to achieve it.

 

easy way is to use $rootScope. Inject $rootscope in to all your widgets where you want it.

 

create a function using $rootscope in any widget .

 

$rootSCope.add=function(){

your functionality

}

and utilize the function wherever you want in your portal by just calling using $rootscope.add().

 

the second and best way to use is create a angular-provider type as service or factory. and create your function over there and using the service or factory u can re use your functions all the place

 

 

So, if I write below function in one widget and call in other widgets will it be accessible to all ?

$rootSCope.add=function(){

your functionality

}

yes. Deppika it will work .

let me give you an detailed example

 

so in widget a

define the client script as below

find_real_file.png

please make sure you injected $rootScope in all the widgets you required it as shown in line number 1.

then in the other widget just call the function as below

find_real_file.png

now call that function on-demand whenever you need it. but make sure to inject the $rootScope as show in the screenshot line number 1

 

Mark it as correct/helpful if it helps