Compadre Compadre - 2 months ago 18
AngularJS Question

Can't inject constant to another module config

The problem is I can't insert constant from one module to another's module config.

Main app:

'use strict';

angular.module('identicaApp', [
'ngRoute',
'identicaApp.common',
'identicaApp.mainPage',
'identicaApp.aboutPage',
'identicaApp.registerPlayer'
]).config(['$locationProvider', '$routeProvider', function ($locationProvider, $routeProvider) {
$locationProvider.hashPrefix('!');

$routeProvider.otherwise({redirectTo: '/main'});
}]);


common.js:

angular.module('identicaApp.common', [])
.constant('ROOT_PATH', '/static/angular/identica/');


and the problem module:

'use strict';

angular.module('identicaApp.mainPage', [
'ngRoute',
'identicaApp.common'
])

.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/main', {
templateUrl: common.ROOT_PATH + 'main_page/main_page.html',
controller: 'MainPageCtrl'
});
}])

.controller('MainPageCtrl', [function() {

}]);


common.ROOT_PATH
is not visible by loader...

Answer

I think you missed to inject the actual constant to your config call (e.g. the same way you inject services to controllers), and then you have to reference it by it's name:

'use strict';

angular.module('identicaApp.mainPage', [
    'ngRoute',
    'identicaApp.common'
])

// Here you must inject your 'ROOT_PATH' constant
.config(['$routeProvider', 'ROOT_PATH', function($routeProvider, ROOT_PATH) {
  $routeProvider.when('/main', {
    templateUrl: ROOT_PATH + 'main_page/main_page.html', // You must reference it by its name (ROOT_PATH)
    controller: 'MainPageCtrl'
  });
}])

.controller('MainPageCtrl', [function() {

}]);
Comments