Angular 1.3.10 Uncaught Error: [$injector:modulerr] - angularjs

The app contains very little at the moment, so this is a basic error. I'm using ui-router. This is what I've got in the various files:
app.module.js:
'use strict';
angular
.module('app', app);
app.$inject = [
'ui.router',
'ngCookies',
'ngSanitize',
];
function app(){}
app.routes.js:
'use strict';
angular
.module('app')
.config(routes);
routes.$inject = ['$stateProvider', '$urlRouterProvider', '$locationProvider'];
function routes($stateProvider, $urlRouterProvider, $locationProvider) {
// Remove # from url
$locationProvider.html5Mode(true);
// Set 404
$urlRouterProvider.otherwise('/404');
// Configure app states
$stateProvider
.state('app', {
abstract: true,
url: '',
templateUrl: 'modules/app/app.html',
controller: 'AppController'
})
}
app.controller.js:
'use strict';
angular
.module('app')
.controller('AppController', AppController);
AppController.$inject = ['$rootScope', '$scope'];
function AppController($rootScope, $scope) {
}
I'm getting the above error in the console on page load - what am I missing?

You are calling $inject on a variable called app, which does not exist in global scope:
angular.module('app', app);
app.$inject = [
'ui.router',
'ngCookies',
'ngSanitize',
];
Now if you would assign your module definition to a variable called app it works:
var app = angular.module('app', app);
app.$inject = [
'ui.router',
'ngCookies',
'ngSanitize',
];
Next the signature for module definition:
angular.module(name, [requires], [configFn]);
Your again using a variable called app (which doesn't exist) where your requires/injectables array should be. So you could do this:
var injections = [
'ui.router',
'ngCookies',
'ngSanitize',
];
var app = angular.module('app', injections);
Or even better: (keeps the global scope clear)
var app = angular.module('app', [
'ui.router',
'ngCookies',
'ngSanitize',
]);
Now you can strip out:
app.$inject = [
'ui.router',
'ngCookies',
'ngSanitize',
];
But it's best practice to keep your entire global scope as clean as possible. So you don't assign your module to variable:
angular.module('app', []);
And when you need it for configuration or attaching controllers or directives, etc, you call:
angular.module('app').config();
angular.module('app').controller();
angular.module('app').directive();
Or you can chain them together:
angular.module('app')
.config()
.controller()
.directive();
Another tip, when doing your configuration or creating a controller, you don't have to do seperate injection, you can simply do:
angular.module('app').config([
'$stateProvider', '$urlRouterProvider', '$locationProvider',
function ($stateProvider, $urlRouterProvider, $locationProvider) {
.....
}
]);
The bracket notation is needed so when you minify your scripts, it will not screw up your injections, if you don't minify you can do without:
angular.module('app').config(
function ($stateProvider, $urlRouterProvider, $locationProvider) {
.....
}
);
I know your problem is solved already, just thought i should clear things up a little. Good luck!

first remove the comma after 'ngSanitize' in app.module.js:
angular
.module('app', [
'ui.router',
'ngCookies',
'ngSanitize'
];

The module didn't like being instantiated like that, so I changed it to this and it works fine:
'use strict';
angular
.module('app', [
'ui.router',
'ngCookies',
'ngSanitize'
];

Related

Injecting Underscore.js into Angular Controller

I tried two of the solutions here to no avail.
This is my Error:
angular.js:68 Uncaught Error: [$injector:modulerr] Failed to instantiate module flavorApplication due to:
Error: [$injector:unpr] Unknown provider: underscore
Here is my code for the module:
var underscore = angular.module('underscore', []);
underscore.factory('_', ['$window', function() {
return $window._;
}]);
Here is my App Config:
(function(){
angular.module("flavorApplication",
['ui.bootstrap',
'ui.router',
'angular-loading-bar',
'angular-confirm',
]);
angular.module("flavorApplication").config(['$stateProvider', '$urlRouterProvider', '$locationProvider',
'underscore', function ($stateProvider, $urlRouterProvider, $locationProvider, underscore){
Here I'm trying to inject it into a Controller (probably where I'm going wrong)
(function () {
'use strict';
angular
.module('flavorApplication')
.controller('UsedSearchesController', UsedSearchesController);
UsedSearchesController.$inject = ['$stateParams', '$state', 'DataService', '_'];
function UsedSearchesController($stateParams, $state, DataService, _) {
var vm = this;
vm.currentSearches = $stateParams.search.split("|")
activate(vm);
////////////////
function activate(vm, _) {
vm.removeSearch = function (searchTerm) {
$stateParams.search = _.filter(vm.currentSearches,
function(search){return search !== searchterm}).join("|")
$state.go('home');
}
}
}
})();
You missed $window dependency to inject in your factory
underscore.factory('_', ['$window', function($window) {
Other thing you can't get factory/service singleton object to be avail at config phase of angular, you can't get that object there.
//remove 'underscore' dependency from config phase like below.
angular.module("flavorApplication").config(['$stateProvider', '$urlRouterProvider', '$locationProvider',
function ($stateProvider, $urlRouterProvider, $locationProvider){
Additionally, you don't need to add _ as a parameter in activate function,
function activate(vm) { //<-- remove _ from here
Don't forget to inject underscore module to flavorApplication
module so that would make available _ object throughout application
modules & components.
angular.module("flavorApplication",
['ui.bootstrap',
'ui.router',
'angular-loading-bar',
'angular-confirm',
'underscore' //<-- added underscore module here
]);

angular unknown provider upload

I have a problem with upload module in Angular. I install module from https://github.com/nervgh/angular-file-upload
I use Angular 1.5.0
In index.html i have:
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-file-upload/dist/angular-file-upload.js"></script>
<script src="scripts/app.js"></script>
My app.js
var app = angular
.module('MyApp', [
'ngAnimate',
'ngCookies',
'datatables',
'ngResource',
'ngRoute',
'angularFileUpload',
'ngSanitize',
'ngTouch'
])
.config(function ($routeProvider) {
$routeProvider...
This is my main.js
angular.module('MyApp')
.controller('MainCtrl', ['$rootScope', '$scope','$upload','$location', 'myService', function ($rootScope, $scope,$upload,$location, myService) {
}]);
In console : Error: [$injector:unpr] Unknown provider: $uploadProvider <- $upload <- MainCtrl
$upload variable is undefined
Please help me.
Just replace $uploader to FileUploader. there is some problem with fileuploader module, and updated FileUploader module using FileUploader service.
angular.module('MyApp')
.controller('MainCtrl', ['$rootScope', '$scope','FileUploader','$location', 'myService', function ($rootScope, $scope,FileUploader,$location, myService) {
var uploader = $scope.uploader = new FileUploader({
url: 'upload.php'
});
//Any other code or processing
}]);
Wrong injector used please check above

Defining myApp and reusing it in controller in service

I am getting javascript run time error
myApp is undefined
for my angular service. Don't know what wrong im doing here..
This is how i defined my app.js
app.js
var myApp = angular.module('myApp', [
'ngRoute', 'motorControllers', 'motorDetailsController', 'motorServices', 'ngSanitize', 'ui.select', 'ngResource',]);
myApp.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/motors', {
templateUrl: 'View/motorList.html',
controller: 'motorController'
}).
when('/motors/:Id', {
templateUrl: 'View/motorDetails.html',
controller: 'motorDetailsController'
}).
otherwise({
redirectTo: '/motors'
});
}]);
This is how i am creating/calling myApp in controller.
Controller
angular.module('myApp', []).controller('motorController', function ($scope, motorService) {
//////
}
This is how i am trying use myApp in my service. but it gives error myApp is undefined.
Service:
myApp.service('motorService', ['$http', function ($http) {
//////
}
and this is how i declared it in my html
<html ng-app="myApp">
Looking forward for some help.
Thanks
I think that the error is in the controller's declaration, because you're defining the app 'myApp' twice.
angular.module('myApp', []).controller('motorController', function ($scope, motorService) {
//////
}
should be
angular.module('myApp').controller('motorController', function ($scope, motorService) {
//////
}
Edit
#simbada
It's up to you. You can separate them in different modules.
(function(angular){
angular
.module('myApp', ['myApp.Controllers']);
angular
.module('myApp.Services', [])
.service('mySrv', function($http){
function _get(){
return $http.get('url');
}
return {
get: _get
}
});
angular
.module('myApp.Controllers', ['myApp.Services'])
.controller('myCtrl', function($scope, mySrv){
$scope.var = 'Hello';
});
})(angular);
You have passed comma in dependancy after ngResource just remove that it'll work fine.
var myApp = angular.module('myApp', [
'ngRoute', 'motorControllers', 'motorDetailsController', 'motorServices', 'ngSanitize', 'ui.select', 'ngResource']);
I think this should be
angular.module('myApp').controller('motorController', function ($sc...
whitout [ ] brackets.
also see how you declare your service:
var myApp = angular.module('myApp', [
'ngRoute', 'motorControllers', 'motorDetailsController', 'motorServices', 'ngSanitize', 'ui.select', 'ngResource']);
when your service is 'motorService'
Your service should be:
myApp = angular.module('motorServices');
myApp.service('motorService', ['$http', function ($http) {
//////
}

injecting dependencies in to an angular app wrapped in a closure

I am having trouble injecting dependancies inside a closure, which wraps around angular. How do I include dependencies inside angular when it is wrapped inside a closure. Angular is being bootstrapped in index.html.
An example accomplishing this in a plunker would help. Here is my code.
Let me know if there are any ambiguities.
(function(angular){
angular.module('myApp', ['restangular', 'ngCookies', 'ngSanitize', 'ngRoute',
'decipher.tags', 'ui.bootstrap', 'ui.bootstrap.typeahead',
'angularMoment', 'textAngular', 'ui.event', 'ui.mask', 'ui.validate',
'toaster', 'ngImgCrop']).
config(function ($httpProvider, $routeProvider,RestangularProvider,
versionedUrl, decipherTagsOptions, mobileAppUrl,
mobileAppDownloadLink,mobileAllowedViews) })();})(angular);
Pass them the way you are passing angular:
(function(angular, dep1, dep2, dep3){
angular.module('myApp', ['restangular', 'ngCookies', 'ngSanitize', 'ngRoute',
'decipher.tags', 'ui.bootstrap', 'ui.bootstrap.typeahead',
'angularMoment', 'textAngular', 'ui.event', 'ui.mask', 'ui.validate',
'toaster', 'ngImgCrop']).
config(function ($httpProvider, $routeProvider,RestangularProvider,
versionedUrl, decipherTagsOptions, mobileAppUrl,
mobileAppDownloadLink,mobileAllowedViews) })();})(angular, dep1, dep2, dep3);

Trying to inject into AngularJS run block

In an attempt to initialize my application, I'm trying to init the module the run() method as follow, but it does not compile.
the error is:
Uncaught Error: [$injector:unpr] http://errors.angularjs.org/1.3.5/$injector/unpr?p0=%24routeProvider%20%3C-%20%24route
Error: error:unpr
Unknown Provider
Unknown provider: $routeProvider <- $route
and here's the code in app.js :
(function () {
'use strict';
angular.module('rage', [
'ui.router',
'ui.bootstrap',
'ui.dashboard',
'kendo.directives',
'jqwidgets'
]).run(['$route', '$rootScope', init]);
function init($route, $rootScope){
var i = 1;
}
})();
However with no dependencies, it runs through fine:
(function () {
'use strict';
angular.module('rage', [
'ui.router',
'ui.bootstrap',
'ui.dashboard',
'kendo.directives',
'jqwidgets' // Kendo UI and jQWidgets libs (loaded in index.html)
]).run(init);
function init(){
var i = 1;
}
})();
$routeProvider is not part of ui.router module. and ui.router does not use ngRoute as well so you cannot access $route service inside the run block because it does not exist. Try including ngRoute if you need to use it (But you already have a ui.router so i am not sure).
angular.module('rage', [
'ngRoute' //<-- Here
'ui.router',
'ui.bootstrap',
'ui.dashboard',
'kendo.directives',
'jqwidgets'
]
Or just remove $route from the dependency list.

Resources