I have several modules in my application, every module definition in separated js file:
angular.module('app', ['app.module1']);
angular.module('app.module1', ['app.module1.module2']);
angular.module('app.module1.module2', []);
And then I want to create controller for last module:
angular.module('app.module1.module2').controller('myController', function(){});
In this case I have error
Module 'app.module1.module2' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
Could anyoune explain where is the problem? Sorry for newbie question.
Note: every definition in it's own js file.
You cant use a module before it is created, so you must be carefull in which order you load then ,load the module definition first then you can reopen the module in a file loaded after the module definition file.
However it makes more sense to stick to one one module per file. That way you dont have to care in which order your module are declared.
so you could write thing instead :
angular.module('app.module1.module2.controllers',[])
.controller('myController', function(){});
then
angular.module('app.module1.module2', ['app.module1.module2.controllers']);
even if you load app.module1.module2 after app.module1.module2.controllers it will work.
Related
var app = angular.module('mittens',['ui.router','ngCookies']);
Isn't above the right way to inject ngCookies? It's throwing this error
Uncaught Error: [$injector:modulerr]
http://errors.angularjs.org/1.4.9/$injector/modulerr?p0=mittens&p1=Error%3A…0zc%20(http%3A%2F%2Flocalhost%3A3000%2Flibrary%2Fangular.min.js%3A20%3A274)(…)
https://docs.angularjs.org/error/$injector/modulerr?p0=mittens&p1=Error:%20%5B$injector:modulerr%5D%20http:%2F%2Ferrors.angularjs.org%2F1.4.9%2F$injector%2Fmodulerr%3Fp0%3DngCookie%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.4.9%252F%2524injector%252Fnomod%253Fp0%253DngCookie%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A3000%252Flibrary%252Fangular.min.js%253A6%253A416%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A3000%252Flibrary%252Fangular.min.js%253A24%253A186%250A%2520%2520%2520%2520at%2520b%2520(http%253A%252F%252Flocalhost%253A3000%252Flibrary%252Fangular.min.js%253A23%253A252)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A3000%252Flibrary%252Fangular.min.js%253A23%253A495%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A3000%252Flibrary%252Fangular.min.js%253A38%253A153%250A%2520%2520%2520%2520at%2520n%2520(http%253A%252F%252Flocalhost%253A3000%252Flibrary%252Fangular.min.js%253A7%253A355)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A3000%252Flibrary%252Fangular.min.js%253A38%253A1)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A3000%252Flibrary%252Fangular.min.js%253A38%253A170%250A%2520%2520%2520%2520at%2520n%2520(http%253A%252F%252Flocalhost%253A3000%252Flibrary%252Fangular.min.js%253A7%253A355)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A3000%252Flibrary%252Fangular.min.js%253A38%253A1)%0A%20%20%20%20at%20http:%2F%2Flocalhost:3000%2Flibrary%2Fangular.min.js:6:416%0A%20%20%20%20at%20http:%2F%2Flocalhost:3000%2Flibrary%2Fangular.min.js:38:427%0A%20%20%20%20at%20n%20(http:%2F%2Flocalhost:3000%2Flibrary%2Fangular.min.js:7:355)%0A%20%20%20%20at%20g%20(http:%2F%2Flocalhost:3000%2Flibrary%2Fangular.min.js:38:1)%0A%20%20%20%20at%20http:%2F%2Flocalhost:3000%2Flibrary%2Fangular.min.js:38:170%0A%20%20%20%20at%20n%20(http:%2F%2Flocalhost:3000%2Flibrary%2Fangular.min.js:7:355)%0A%20%20%20%20at%20g%20(http:%2F%2Flocalhost:3000%2Flibrary%2Fangular.min.js:38:1)%0A%20%20%20%20at%20db%20(http:%2F%2Flocalhost:3000%2Flibrary%2Fangular.min.js:41:272)%0A%20%20%20%20at%20c%20(http:%2F%2Flocalhost:3000%2Flibrary%2Fangular.min.js:19:463)%0A%20%20%20%20at%20zc%20(http:%2F%2Flocalhost:3000%2Flibrary%2Fangular.min.js:20:274
app.controller('HomeController',['$scope','$http','$cookies',function($scope,$http,$cookies) {}
You misspelled ngCookies as ngCookie in your real code but corrected it for the question.
Did you click on the admittedly rather long URL?
That will tell you:
Failed to instantiate module mittens due to:
and then another link which tell you:
Failed to instantiate module ngCookie due to:
and then another link which takes you to:
Module 'ngCookie' is not available! You either misspelled the module
name or forgot to load it. If registering a module ensure that you
specify the dependencies as the second argument.
Each of these is accompanied by a stack trace, but the descriptions I've quoted at the top really say it all. ngCookie is not available because either you forgot to load the javascript or you misspelled its name.
Now there seems to be some confusion here as your question says you asked for 'ngCookies', but the error message says 'ngCookie' was not found. Check that you didn't autocorrect a misspelling of the name when you posted the question.
Because you forget to load the angular cookies file. Load th file first and then try.
<script src="path/to/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-cookie/4.0.0/angular-cookie.js"></script>
Finally, load the module in your application by adding it as a dependent module:
angular.module('app', ['ngCookies']);
With that you're ready to get started!
Make sure you have refereed library
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-cookies.js" type="text/javascript"></script>
Make sue you have injected the dependency
var miAp = angular.module('miAp', ['ngCookies']);
DEMO
I have this error:
Uncaught Error: [$injector:nomod] http://errors.angularjs.org/1.3.15/$injector/nomod?p0=sensorManagement
sensorManagement is my module name.
Any idea what the error above mean?
If you follow the link you'll see:
Module 'sensorManagement' is not available! You either misspelled the
module name or forgot to load it. If registering a module ensure that
you specify the dependencies as the second argument.
That means you did not declare the module, which you can fix like so:
angular.module('sensorManagement', []);
It's also possible that you have the problem as answered by Amo_Geismar, and you have the above multiple times (or forgot to load the js file).
To work with a module after declaring it, you leave out the array.
sensorManagement.module.js
angular.module('sensorManagement', []);
someService.service.js
angular.module('sensorManagement') // Notice the lack of ', []' here
.factory('yourService', function() {
// Code here...
});
possible causes:
1) In essence you are creating the same module more than once.
I think you probably have this somewhere in your code multiple times:
angular.module('sensorManagement',[])
if you want to use the module do
angular.module('sensorManagement'). //chain whatever controller/filter/service/factory
2) you have forgotten to load the script where you declared the module so probably in your index.html you are missing
<script src="your/modules/sensorManagement.js"/>
which would contain your declaration:
angular.module('sensorManagement',[])
I have a global module 'app' which include two other modules 'app.core' and 'app.service'. This is basic settings. Inside the two sub module I can access the constant from both two modules. However I cannot access constants declared in 'app' within 'app.core' or 'app.service'.
Also I use angular.bootstrap to delay my initialization because I need to retrieve a config from server. But after receiving the config I do angular.module('app').constant('config', config); So the constant should be well defined..
LAst point, inside the 'app' module config I can access the config constant.
Any idea ?
'app' module constants declaration
angular.module('app',[
'app.core',
'app.service'
])
.module('app')
.constant('foo', 'foo')
'app.core' constants
angular
.module('app.core')
.constant('core', 'core');
In 'app.service' I can get core constant
angular.module('app.services',[]).config(function($injector){
console.log($injector.get('core'));
})
But I cannot retrieve 'app' constant
angular.module('app.services',[]).config(function($injector){
console.log($injector.get('foo'));
})
Will crash
In both configurations you are trying to access a constant defined within a separate module, but then not defining that module as a dependency. How, for example, can app.services have access to foo when foo is defined on a module which requires app.services in the first place?
The reason that core is available to app.services despite this is because you have listed the dependencies in such an order when defining app that angular happens to have loaded app.core prior to app.services. The order of the defined dependencies, however, should not matter.
In order to correct this, you should consider refactoring your modules so that there is no inherent circular dependency. For example, consider making your config a module in itself and inject it into the dependent services:
angular.module('app', ['app.core', 'app.services'])
angular.module('app.config', [])
.constant('foo', 'foo')
angular.module('app.core', ['app.config'])
.config(function(foo) {})
angular.module('app.services', ['app.config'])
.config(function(foo) {})
Note also that using the injector to get constants is un-necessary as they can be injected directly during the configuration stage.
I'm having a really hard time trying to make modules working on an app I'm building.
This is the main file
main.js
'use strict';
angular.module('clientPortalPublic',[
'ngCookies',
'ngResource',
'ngAnimate',
'clientPortalPublic.components'
]);
angular.module('clientPortalPublic.components',[]);
And I have another file switch-login-effect.js
'use strict';
angular.module('clientPortalPublic.components').directive('switchLoginEffect',['$timeout', function($timeout){
//Content removed for clarification
}]);
The order that those files are being loaded is:
<script type="application/javascript" src="public/components/switch-login-effect.js"></script>
<script type="application/javascript" src="public/main.js"></script>
I know the switch-login-effect.js should be loaded later, since is requiring the main module, but it's being loaded dynamically and I don't control the order. BUT using manual bootstrapping shouldn't angular deal with it?
This is how I'm bootstrapping it
angular.element(document).ready(function() {
angular.bootstrap(document, ['clientPortalPublic']);
});
If I run the code above I get:
Error: [$injector:nomod] Module 'clientPortalPublic.components' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
Thanks!
You are declaring a directive on a non-existant module when switch-login-effect.js loads first. It looks like you are trying to dynamically control what elements are included in the clientPortalPublic.components module simply by adding or removing scripts, but I don't think angular's dependencies are set up for that. A main reason to have those dependencies is to know exactly what you are getting.
The clientPortalPublic.components module should be defined in one script file if possible. If you have various components you can create different modules for each, but the definition of your application module should know what it is getting by the dependencies it requires. That would cause debugging headaches for one reason, "Why is my directive not working? I'm loading the components module..." (but you missed a script file you have no way to know that you need)
I really don't advise creating your app this way, but if you are dead-set you could catch the error and create the module at the start of each individual component file (and in your main.js in case you don't actually have any components but still want to require the module) so it doesn't matter which one is loaded first:
try {
angular.module('clientPortalPublic.components');
} catch (err) {
angular.module('clientPortalPublic.components',[]);
}
Or more simply just uses javascript to see if it's been executed:
var componentsModule = componentsModule ||
angular.module('clientPortalPublic.components',[]);
After reading some angular good practices and paying more attention to angular seed, I have it working.
THe thing is that they recommend to do the following when you have an structure similar to:
app/
app/components
app/components/component1
app/components/component2
app.js => angular.module('main',['main.components']);
app/components/components.js => angular.module('main.components',['main.components.component1', 'main.components.component2']);
app/components/component1.js => angular.module('main.components.component1',[]);
app/components/component2.js => angular.module('main.components.component2',[]);
Having that structure make sense and works perfectly.
:)
ngMock does some magic to automatically include itself if you include angular-mocks.js in your index.html.
What's the simplest way to force angular to load a module in test mode simply by including a file and not having to edit any module dependencies.
The only way to load a module is by calling angular.module(...). ngMocks loads "itself" by calling:
angular.module('ngMock', ['ng']).provider(...).config(...);
You don't need to declare a module as a dependency to load it. You can just include angular.module('<moduleName>', [<moduleDependencies>...]); in its script.
If you mean "how is ngMock automagically added to the dependency list of any module loaded using window.module or angular.mock.module, it is because ngMocks creates a custom injector, such that it takes the list of dependencies and prepends 'ngMock':
window.inject = angular.mock.inject = function() {
...
return isSpecRunning() ? workFn() : workFn;
...
function workFn() {
var modules = currentSpec.$modules || [];
modules.unshift('ngMock'); // <-- this line does the trick
modules.unshift('ng');
...
You could create your own function that prepends your module in the list of dependencies before instantiating, but I hardly believe this will help in testing. On the contrary, it will be one more source of errors (and will result in possibly "hiding" dependency errors).