1

How to declare global variable in angularjs app.js ?

1

2 Answers 2

1

You can use contants which can be injected anywhere in the app.

app.constant('MOVIE_TITLE', 'The Matrix');

Or if it's a value that is likely to change, use values

app.value('movieTitle', 'The Matrix');

values can't be injected in config though.

Sign up to request clarification or add additional context in comments.

Comments

0

The first thing that would come to your mind would be declaring variables in $rootScope. But's that's a very bad idea and should be avoided. You can use factories or services, but that's a little bit complex. The easiest way would be using Constants and Values. Refer the below article:

https://ilikekillnerds.com/2014/11/constants-values-global-variables-in-angularjs-the-right-way/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.