I'm trying to do something like this:
angular.module('MyModule', ['ui'])
.config(function($rootScope) {
$rootScope.Gender = {
'M': 'Male',
'F': 'Female',
'U': 'Unknown',
};
})
But I get this error:
Uncaught Error: Unknown provider: $rootScope from MyModule
If I can't access $rootScope
inside my module config, where's the proper place to initialize module-wide variables?
(If you make a variable inside the Python interpreter, and then import other modules, your variable is in the outermost scope and thus global within your Python session.) All you have to do to make a module-global variable is just assign to a name.
Instead of using $rootScope
, you could also use an angular constant or value:
angular.module('MyModule', ['ui']).constant( 'Gender', {
'M': 'Male',
'F': 'Female',
'U': 'Unknown',
});
A constant can never be changed, whereas a value can. And you can inject it wherever you need it:
app.controller( 'MainController', function ( $scope, Gender ) {
console.log( Gender.M );
});
In my opinion. this seems more "proper" for site-wide variables than using $rootScope
.
You can't inject services (here $rootScope) into config block. Only constants and providers can be injected during the config phase.
In your case the correct solution would be to use the run block. Just change config to run and things should be working as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With