Error while injecting ngCookies? - angularjs

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

Related

Angularjs: missing dependency

When I launch my angularjs application I am getting this error
Uncaught Error: [$injector:nomod] Module 'App' 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. http://errors.angularjs.org/1.3.11/$injector/nomod?p0=App
As I have about 20 dependencies. How can I know which dependency is not satisfied?
angular.module('myApp', ['LocalStorageModule', 'tmh.dynamicLocale','ngResource', 'ui.router',
'ngCookies','pascalprecht.translate', 'ngCacheBuster', 'ngTable',
'ngSanitize','ui.select','angularValidator','ui.bootstrap','googlechart',
'ui.bootstrap.showErrors','ngActivityIndicator','ncy-angular-breadcrumb',
'anguFixedHeaderTable', 'ui.utils','io.dennis.contextmenu'])
Angular is trying to load a module called app. As your code looks like, you define your module name as myApp - so just rename one of them to archieve name equality.
EG:
angular.module('app', ['LocalStorageModule', 'tmh.dynamicLocale','ngResource', 'ui.router',
'ngCookies','pascalprecht.translate', 'ngCacheBuster', 'ngTable',
'ngSanitize','ui.select','angularValidator','ui.bootstrap','googlechart',
'ui.bootstrap.showErrors','ngActivityIndicator','ncy-angular-breadcrumb',
'anguFixedHeaderTable', 'ui.utils','io.dennis.contextmenu'])
I don't think this is a dependency issue but simply a naming issue. Make sure your ng-app statement is correct in your HTML. By the looks of it your statement is probably ng-app="App" instead of ng-app="myApp".

What is meaning of error: Uncaught Error: [$injector:nomod]

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',[])

How to inject a module and make it accesible to all modules and submodules of app (getting unknown provider error)

Please note this is not a duplicate of:How to inject module and make it accesible to entrie angular app
I have a module (app.config) that I would like to inject into my entire app.
The module needs to be accessible within all other modules and submodules(modules inside modules) injected into myApp
For example, my app looks like this:
angular.module('myApp', [
'app.config',
'module#1',
'module#2',
'module#3',
'module#4'
])
.config...
/////////////////////////////////
Here's app.config
angular.module('app.config', []).
constant('NAME1', 'Name1').
constant('NAME2', 'Name2');
////////////////////
I want 'app.config' injected in such a way that it should be accesible in module#500 which is not directly a dependecy of 'myApp', but a dependecy of module#1 - which, in turn, is a dependecy of myApp.
module#1 is defined as such (as shown,module#500 is a dependecy of module#1):
angular.module('module#1', [
'module#500',
'module#501',
'module#502',
...
]);
Here's my problem:
angular.module('module#500', []).
service('serviceOne', serviceOne);
function ServiceOne($http, NAME1) {
var service = {
getMyProfile: function(){return $http.get('api/' + NAME1);}
};
return service;
}
Problem - I get an error-> Uncaught Error: [$injector:unpr] Unknown provider: NAME1Provider <-NAME1 <- serviceOne But I thought I injected it to the entire app???
I don't want to add module#500 directly as dependency to 'myApp' I wan't to leave module#500 as a dependecy of module#1. And I want to leave module#1 as a dependency of myApp
I don't want to individually inject app.config to each and every module either. Any other solutions?
I don't want to individually inject app.config to each and every
module either. Any other solutions?
I don't know what solution you could be expecting? With this line: angular.module('module#500', []). how is module#500 going to know about anything else, it has nothing given to it. Maybe I'm misunderstanding something here.
edit, as I've just read your post and comments from yesterday: I think you're not understanding dependency-injection properly. It only goes one way, module#1 has access to module#500, but module#500 doesn't have access to module#1. It has to be that way: How could you unit-test module#500 if it has some behavior that depends on module#1, which is not mentioned anywhere in its code?
I'm guessing your project doesn't really call for so many modules, if they all depend on the same config variables. Those factories and services that depend on the config variables should be in the same module with it. That is proper compartmentalization and will make your life easier in the long run.
If you don't want to make myApp dependent on app.config (though it would be the right thing to do because its submodules depend on it), you can load config module with manual bootstrapping
angular.bootstrap(document, ['myApp', 'app.config']);
instead of ng-app="myApp".
You forgot to inject NAME1 in your ServiceOne service.
eg: function ServiceOne($http, NAME1)

AngularJS loading modules

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.
:)

Multiple modules loading error

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.

Resources