AngularJS, cannot add modules - javascript

So I am quite new to AngularJS and I can't seem to add additional module lets say for example d3 for graph.
So My code for adding new modules is
(function() {
'use strict';
angular
.module('infographicsModule', ['ngAnimate', 'ngCookies', 'ngTouch', 'ngSanitize', 'ngMessages', 'ngAria', 'ngResource', 'ui.router', 'ui.bootstrap', 'toastr']);
})();
But if I try to add d3 as an additional module it throws an error
Uncaught Error: [$injector:modulerr] Failed to instantiate module infographicsModule due to:
Error: [$injector:modulerr] Failed to instantiate module d3 due to:
Error: [$injector:nomod] Module 'd3' 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.
Any suggestions? If you require more code, I can post it.

Maybe it's happen cause D3 isn't an angular module.
You can try do this with the following link: https://github.com/WealthBar/angular-d3
Or you can encapsulate in a service or factory and call from this.

Related

angular-sweetalert + browserify: how to install/require/inject?

I installed angular-sweetalert ^1.1.2 via npm install --save angular-sweetalert and tried to inject it:
var app = angular.module('myApp', [
require('angular-sweetalert') // doesn't work
]);
I'm getting the following Error:
Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed to instantiate module undefined due to:
Error: [ng:areq] Argument 'module' is not a function, got undefined
It seems like it's depending on more:
include the files in your app
i. SweetAlert.min.js
ii. sweet-alert.js OR sweet-alert.min.js
include the module in angular (i.e. in app.js) - oitozero.ngSweetAlert
Any ideas how to properly install/require the module?
Thanks in advance!
The module injector name is wrong, it should be 'oitozero.ngSweetAlert' rather than 'angular-sweetalert', also don't use require in the module injector. From angular.module says
A module is a collection of services, directives, controllers,
filters, and configuration information. angular.module is used to
configure the $injector.
var app = angular.module('myApp', [
'oitozero.ngSweetAlert'
]);
PS: Make sure all the dependency files (step 2) are included before inject the library module.

Angular Failed to instantiate module after renaming it

I had an Angular module named "sample" and angular debugging console was asking me to rename it to "appname.sample", I did that and I also changed the calling of the module.
But angular seems to be still looking for module sample. and I am getting the error:
$injector:modulerr] Failed to instantiate module decisionone due to: Error: [$injector:nomod] Module is not available! You either misspelled the module name or forgot to load it
Where else am I supposed to change it?
Here is the code:
angular.module('appname.sample', ['ngRoute', 'common', 'ngSanitize'])
and when I am including it:
angular.module('appname.sample')
If you have your config file/route file for that module some where else. Make sure you are also changing that module name.
For example in ngRoute if config and rout specification are defined separately I also need to make module name to be same.
angular.module('appname.sample', []);
angular.module('appname.sample').config(['$routeProvider', function($routeProvider) {
$routeProvider
.when("/urlPath", {
controller: 'controllerName',
templateUrl: 'modules/pathToViewPage/view.html',
});
}
]);
In your index.html file page you should have ng-app="your module name" on a tag somewhere, probably the body tag. Change this to match your new module name.
Could it be maybe that you made already a different module dependent on your sample module something like this:
angular.module('someDifferentModule', ['sample'])
If so you have to rename it here as well:
angular.module('someDifferentModule', ['appname.sample'])
Also, based on the Angular error, it seems that the problem is not with your app.sample but rather with a module named decisionone
$injector:modulerr] Failed to instantiate module decisionone due to:
Error: [$injector:nomod] Module is not available! You either
misspelled the module name or forgot to load it
Are you injecting this some where like this:
angular.module('someModule', ['decisionone'])
and desicionone was never instantiated as a module?

Failing to instantiate ngRoute for a View

I am using below piece of code in my JS file:
var mainAngular = angular.module('baniyaApp', ['ngRoute','ui.bootstrap']);
////======================== CONFIG =================================
mainAngular.config(function($routeProvider, $locationProvider) {
alert('ng route');
$routeProvider
.when('/', {
templateUrl: "HomeLandPage.html"
});
});
////=================================================================
"HomeLandPage.html" exists at the same location, so no issue with the path. The main page that contains ng-view in it.I have taken care of using angular.js file from http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.js link.
I am getting the below error on the console. Please confirm that the JavaScript file is correct.
Uncaught Error: [$injector:modulerr] Failed to instantiate module baniyaApp due to:
Error: [$injector:modulerr] Failed to instantiate module ngRoute due to:
Error: [$injector:nomod] Module 'ngRoute' 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.
You need to include angular-routes.js after including angular.js in your app directory and html

Grunt build failing after adding angular-leaflet-directive

I've just added angular-leaflet-directive to my project and when I come to build it with Grunt, it's now failing. The dependency was added using bower. This project was built using the Yeoman angular-generator.
Here I'm including the leaflet-directive in my app.js
angular
.module('statsApp', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute',
'leaflet-directive'
])
And then for now, I simply have <leaflet></leaflet> in my view just to get things started.
When the Grunt build fails, I get this error message
Error: [$injector:modulerr] Failed to instantiate module statsApp due to:
Error: [$injector:modulerr] Failed to instantiate module leaflet-directive due to:
Error: [$injector:nomod] Module 'leaflet-directive' 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.
This SO post solved my problem. It's not specific to the module I'm adding, but specific to Karma tests failing because they didn't know about the leaflet-directive module.
AngularJS Error: Module ngAnimate is not available

"Failed to instantiate module ngSanitize" when pre-rendering AngualrJS site with PhantomJS

I'm attempting to pre-render my AngularJS site using PhantomJS. (With phantomjs-runner.js from http://www.yearofmoo.com/2012/11/angularjs-and-seo.html) I'm unable to load the page through PhantomJS as the error below occurs. This error does not occur in IE/Chrome/Firefox.
How do I go about fixing this error?
Error:
Error: [$injector:modulerr] Failed to instantiate module SpaceForAfrica due to:
Error: [$injector:modulerr] Failed to instantiate module dialogs due to:
Error: [$injector:modulerr] Failed to instantiate module ngSanitize due to:
Error: [$injector:nomod] Module 'ngSanitize' 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.2.1/$injector/nomod?p0=ngSanitize
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.js:1507
at ensure (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.js:1435)
at module (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.js:1717)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.js:3527
Module config
var SpaceForAfrica = angular.module('SpaceForAfrica', ['ngRoute', 'HashBangURLs', 'ui.bootstrap', 'ui.bootstrap.tpls', 'google-maps', 'ui.growl', 'dialogs', 'ngSanitize', 'angularSpinner','angulartics', 'angulartics.google.analytics']).config(spaceForAfricaConfig);
It looks like you might be missing a reference to the ngSanitize code. ngSanitize is part of the AngularJS framework (https://docs.angularjs.org/api/ngSanitize), however, you have to include a separate reference before your PhantomJS reference in order to utilize it.
It appears you are using version 1.2.1 of AngularJS, so you could easily just add one of these tags (or grab the code to include in your own app.)
Non-minified:
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular-sanitize.js"></script>
Minified:
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular-sanitize.min.js"></script>

Categories

Resources