1

I have two AngularJs Modules, one is my app the other is a list of constants. I want to inject the constant module into my app module and use the constants inside my controllers. I have been able to achieve this inside a factory but I can't access my constants inside my controller.

app module:

(function() {
    'use strict';

    angular.module('myapp', [
            'constants',
            'ngMaterial',
            'ngCordova',
            'ngStorage',
            'relativeDate',
            'ui.router',
            'myapp.services.myservice'
            ])

constant module:

angular.module('constants', [])

.constant('appName', 'mynewapp');

In my factory where I can access my constants:

(function() {
    'use strict';

    angular.module('myapp.services.myservice', [])

        .factory('Myservice', function ($http, appName) {
           console.log(appName); //works great!

My controller where I CANNOT access constants

(function() {
    'use strict';

    angular.module('myapp')
        .controller('MyController', [
            '$cordovaOauth',
            '$state',
            '$rootScope',
            'Myservice',
            MyController
        ]);

    function MyController($cordovaOauth, $state, $rootScope, Myservice, appName ) {
            console.log(appName); //undefined

1 Answer 1

4

You forgot to declare dependency injection token for appName constant:

.controller('MyController', [
    '$cordovaOauth',
    '$state',
    '$rootScope',
    'Myservice',
    'appName',  // <---- don't forget me
    MyController
]);
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to approve good answers, especially when it is clearly the right answer, and others can't make a better answer.

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.