Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write global functions to use in all components in angular

Tags:

People also ask

What is global variable in angular?

We always need to declare global variable file for our angular 8 application because we can set some global variable there like site title, api url etc so you can easily access any where in our application easily. So, in this example, we will create GlobalConstants file for adding global variables in our application.

What is global function in JavaScript?

The global object in JavaScript is an always defined object that provides variables and functions, and is available anywhere. In a web browser, the global object is the window object, while it is named global in Node. js. The global object can be accessed using the this operator in the global scope.


Note : Its not for global variable but for a global common function to perform a functionality on all components

I am working on an angular app where I have around 400 components in different modules, almost all components have one same kind of functionality as mentioned below

There is a sections on many pages which shows a "How to work section" which can be closed by users and will remain closed unless they open it again, I have done it with cookies which I set on click on close or open icon but this function is written in a component and this needs to be imported in other components

I want to create a functions somewhere which perform this functionality on click on icon and can be called without importing any component in others.

One way to do it ( as I thought ) could be create a JavaScript function in a file and load it in index file and then call this function on click on close and open icon

Not sure if this is the best way to do this. Hope someone will come up with a better solution.