How can I access constants in another file from my app.js file?

1k Views Asked by At

I need a separate .js file which I can store in some constants.

I need to use the constants in this file in the app.js file.

The structure I am using for this and the mistake that I have encountered is like the one below.

What is the cause and solution of this problem for you? How can I best handle this situation? (doesnt matter 'value' or 'constant')

"Error: [$injector:unpr] Unknown provider: globalValueProvider <- globalValue <- AppController

index.html

<script src="app/app.js" type="text/javascript"></script> //there it is

<script src="app/globalConstants.js" type="text/javascript"></script>
<script src="app/globalValues.js" type="text/javascript"></script>

app.js

var MyApp= angular.module("MyApp",
[
    //is necessary?
    //depency injection
    ...
]);

const.js

var MyApp= angular.module("MyApp");

MyApp.constant("globalConstant",
{
    "data": "test" // all data, source, file whatever u say that's all.        
});

value.js

var MyApp= angular.module("MyApp");

MyApp.constant("globalValue",
{
    "data": "test" // all data, source, file whatever u say that's all. 
});

controller(in app.js)

MyApp.controller("AppController",
[
    "$scope", "$rootScope", "globalValue", "globalConstant",
    function($scope,
        $rootScope,            
        globalValue,
        globalConstant) {
        debugger;
        console.log(globalValue);
        console.log(globalConstant);
    }
]);
1

There are 1 best solutions below

1
On

Solved. Check your view script tags. app.js must on top anothers. Thanks to @Mr_Perfect