Service portal - How to declare a global constant value ?

Community Alums
Not applicable

Hi community,

I'm looking for the best way to declare a shared constant (in my case, an Array) and use it within every widgets (client script) of my Service Portal.

I found an angular solution but I'm not sure it's possible in sp :

// Storing a single constant value
var app = angular.module('myApp', []);

app.constant('appName', 'My App');

// Now we inject our constant value into a test controller
app.controller('TestCtrl', ['appName', function TestCtrl(appName) {
  console.log(appName);
}]);

Thanks in advance

1 ACCEPTED SOLUTION

iurii_raboshuk
Giga Contributor

Hi,



You can add your constant directly to the Service Portal's main module in your portal's theme JS Include like this:


angular.module("sn.$sp").constant("appName", "My App");



After that appName becomes available for injection in your application. You can use it in all controllers and other functions that support dependency injection in Angular.



Regards,


Iurii


View solution in original post

4 REPLIES 4

chirag_bagdai
ServiceNow Employee
ServiceNow Employee

Hi,



1. You can use rootScope to communicate between widgets, below reference links can be helpful :


https://www.youtube.com/watch?v=Wcz0PixUneQ&index=7&list=PL3rNcyAiDYK3YiwkTj8OUSs_FRz-YSIw2


https://community.servicenow.com/community/develop/blog/2016/08/19/using-rootscope-to-share-data-in-...



2. You can also go for HTML5 localStorage option (not best practise)



Regards,


Chirag Bagdai


Community Alums
Not applicable

Thanks you Chirag.


I agree with you. The rootscope allows to discuss with other widgets in real time.


For my part, I'm looking for a way to declare global constants for the portal and not in a particular widget. Widgets must be independent.



I have develop a script and a dependency with the widget for shared functions and it works fine.


Now, I would like to use the same thing to declare constants and variables but I don't know how.



Any idea ?


chirag_bagdai
ServiceNow Employee
ServiceNow Employee

I am not sure about angular but you can try to declare constant/global variable in UI script and include that UI script in your portal theme (JS Include).



I hope this helps.



Regards,


Chirag


iurii_raboshuk
Giga Contributor

Hi,



You can add your constant directly to the Service Portal's main module in your portal's theme JS Include like this:


angular.module("sn.$sp").constant("appName", "My App");



After that appName becomes available for injection in your application. You can use it in all controllers and other functions that support dependency injection in Angular.



Regards,


Iurii