Asaf Asaf - 2 years ago 136
AngularJS Question

AngularJS - Getting Module constants from a controller

I'm trying to build a myApp.config module to store some settings for my app, I wrote a config.js file:

angular.module('myApp.config', [])
.constant('APP_NAME','My Angular App!')

I added it to my app.js (angular-seed):

angular.module('myApp', ['myApp.filters', '', 'myApp.directives', 'myApp.controllers', 'myApp.config']).

I added it to the index.html file, and now I'm trying to figure out how to get it in my controllers, I tried:

angular.module('myApp.controllers', ['myApp.config'])
.controller('ListCtrl', ['$scope', 'myApp.config', function($scope, $config) {
$scope.printme = $config;

but I'm getting:

Unknown provider: myApp.configProvider <- myApp.config

I'm probably doing something wrong here, any ideas ?

Answer Source

I don't think it is valid to use the module name in an injection like that. You can simply inject the constants by name, though:

angular.module('myApp.controllers', ['myApp.config'])
  .controller('ListCtrl', ['$scope', 'APP_NAME', function($scope, appName) {
     $scope.printme = appName;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download