
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 09:25 PM
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-09-2022 11:36 PM
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);
};

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 09:51 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 10:15 PM
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
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 11:15 PM
yes. Deppika it will work .
let me give you an detailed example

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 11:26 PM
so in widget a
define the client script as below
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
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