AngularJs injection error - angularjs

function MainCtrl($scope, $http, $interval, $state, $location)
{
}
angular
.module('inspinia', ['angularCharts'])
.controller('MainCtrl', MainCtrl)
No matter what I try to inject, I get an error. Instructions are not very clear. For instance, should I be pulling in the script which has the injection before or after Angur itself? Etc, etc
Is there anything obviouslty wrong with that code above?
IMPORTANT this is an exisitng, fuctcioning app. As pointed out in the comments, the error seems clear, BUT it only occurs when I add , ['angularCharts']. Before that, the coude is working just fine, routing, state chanegs and all.
I suspect that the erorr message is a red herring. The problem obvoulsy stems from adding , ['angularCharts'] - but why?
Error: [$injector:unpr] Unknown provider: $stateProvider <- $state <- MainCtrl
http://errors.angularjs.org/1.5.0/$injector/unpr?p0=%24stateProvider%20%3C-%20%24state%20%3C-%20MainCtrl
at angular.js:68
at angular.js:4397
at Object.getService [as get] (angular.js:4550)
at angular.js:4402
at getService (angular.js:4550)
at injectionArgs (angular.js:4574)
at Object.invoke (angular.js:4596)
at extend.instance (angular.js:9855)
at nodeLinkFn (angular.js:8927)
at compositeLinkFn (angular.js:8226)(anonymous function) # angular.js:13236(anonymous function) # angular.js:9965Scope.$apply # angular.js:16925bootstrapApply # angular.js:1694invoke # angular.js:4604doBootstrap # angular.js:1692bootstrap # angular.js:1712angularInit # angular.js:1606(anonymous function) # angular.js:30423j # jquery-2.1.1.min.js:2k.fireWith # jquery-2.1.1.min.js:2n.extend.ready # jquery-2.1.1.min.js:2I # jquery-2.1.1.min.js:2
[Update] I am basing the app on a framework. I don't know how good it is, but it does work (state change and all).
INdex.html has <script src="js/app.js"></script>
which file contains the following. Maybe I should be adding ['angularCharts'] there? In fact, is it wrong of me to have that .module line at the end of my controller?
(function () {
angular.module('inspinia', [
'ui.router', // Routing
'oc.lazyLoad', // ocLazyLoad
'ui.bootstrap', // Ui Bootstrap
])
})();

The problem is that you're redefining the main module of your application when doing
angular.module('inspinia', ['angularCharts'])
So the module loses all its components previously added, and all its dependencies (ui-router, etc.).
Instead, you must modify the axisting definition of the module to add a dependency on angularCharts, and, in your controller, simply retrieve the previously defined module:
angular.module('inspinia').controller(...);

Related

AngularJS unknown provider after code refactoring

I have an app which worked great, until I decided to refactor the code and use Gulp for my project. Here is the error I get:
Error: [$injector:unpr] Unknown provider: _Provider <- _ <- utilsFactory <- storageService <- httpRequestInterceptor <- $http <- $templateFactory <- $view <- $state
Since I'm not sure how to read this error and if they all have an issue or if it's only one of them, here are the two places where I use some of these providers when I start my app:
app.run(function($rootScope, $state, storageService, modalsFactory, appFactory, _) { ... }
And:
angular.module("mediaControl").controller("loginCtrl", function ($state, $translate, authService, utilsFactory, appFactory, storageService) { ... }
In my index.html file, I have:
<!-- inject:js -->
<script src="src/mediaControl.js"></script>
...
<script src="src/login/providers/httpRequestInterceptor.service.js"></script>
...
<script src="src/common/providers/utils.factory.js"></script>
<script src="src/common/providers/storage.service.js"></script>
...
<script src="src/login/login.controller.js"></script>
...
<!-- endinject -->
What I checked/did:
Check if Gulp put all of the necessary js files in index.html and in the good order (seems ok to me)
Check the names of the providers (I didn't change them during the refactoring though)
Try to remove my custom providers from loginCtrl and app.run (I still have the same error when I'm doing this)
I read the documentation and some SO questions, but I don't see anything that I would do wrong, considering that my app worked before the refactoring (which consists in reorganizing my file structure, renaming the files, and starting to rewrite the code using ES6 standards). Any idea about what could be wrong here?
As noticed by #shaunhusain, the lodash provider was missing in my index.html. I fixed my Gulpfile so that the file is injected as well, and it works.

AngularJS Template not properly loading

Had a bit of trouble with Angular earlier, but I was able to resolve that problem. Of course, being a newbie, we've hit more problems!
Did some searching around for this error, but applying the fixes that other people have tried has not proven effective for me.
I am currently trying to load a partial onto a show page in Rails using AngularJS.
EDIT: I apologize- I forgot to mention that I intend to load a partial from a file in the same directory as all these files. Current directory tree is as follows:
Directory containing relevant js files and the timer partial
I have the following code built out:
Show.html.erb
<timer-info ng-controller = "CountdownController">
</timer-info>
app.js
'use strict';
var myApp = angular.module("summoners-universe", ['ngRoute'])
directives.js
'use strict';
myApp.directive("timerInfo", function(){
return {
restrict: "E",
templateUrl: 'app/assets/javascripts/timer.html'
};
});
controllers.js
'use strict';
myApp.controller('CountdownController',
["$scope", "$http", "$routeParams",
function($scope, $http, $routeParams){
console.log($routeParams);
}]);
Eventually this controller will display a countdown timer, but for now, I just wanted to make sure I could get my routeParams and all that.
Sadly, I'm hit with the following error upon page load:
angular.self-208d6d1….js?body=1:11757 GET http://localhost:3000/games/app/assets/javascripts/timer.html 404 (Not Found)(anonymous function) # angular.self-208d6d1….js?body=1:11757sendReq # angular.self-208d6d1….js?body=1:11518serverRequest # angular.self-208d6d1….js?body=1:11228processQueue # angular.self-208d6d1….js?body=1:15962(anonymous function) # angular.self-208d6d1….js?body=1:15978Scope.$eval # angular.self-208d6d1….js?body=1:17230Scope.$digest # angular.self-208d6d1….js?body=1:17046Scope.$apply # angular.self-208d6d1….js?body=1:17338bootstrapApply # angular.self-208d6d1….js?body=1:1750invoke # angular.self-208d6d1….js?body=1:4666doBootstrap # angular.self-208d6d1….js?body=1:1748bootstrap # angular.self-208d6d1….js?body=1:1768angularInit # angular.self-208d6d1….js?body=1:1653(anonymous function) # angular.self-208d6d1….js?body=1:30864fire # jquery.self-4e23968….js?body=1:3188self.fireWith # jquery.self-4e23968….js?body=1:3318jQuery.extend.ready # jquery.self-4e23968….js?body=1:3537completed # jquery.self-4e23968….js?body=1:3553
angular.self-208d6d1….js?body=1:13551 Error: [$compile:tpload] Failed to load template: app/assets/javascripts/timer.html (HTTP status: 404 Not Found)
http://errors.angularjs.org/1.5.5/$compile/tpload?p0=app%2Fassets%2Fjavascripts%2Ftimer.html&p1=404&p2=Not%20Found
at angular.self-208d6d1….js?body=1:69
at handleError (angular.self-208d6d1….js?body=1:18979)
at processQueue (angular.self-208d6d1….js?body=1:15962)
at angular.self-208d6d1….js?body=1:15978
at Scope.$eval (angular.self-208d6d1….js?body=1:17230)
at Scope.$digest (angular.self-208d6d1….js?body=1:17046)
at Scope.$apply (angular.self-208d6d1….js?body=1:17338)
at done (angular.self-208d6d1….js?body=1:11573)
at completeRequest (angular.self-208d6d1….js?body=1:11779)
at XMLHttpRequest.requestLoaded (angular.self-208d6d1….js?body=1:11712)(anonymous function) # angular.self-208d6d1….js?body=1:13551(anonymous function) # angular.self-208d6d1….js?body=1:10226processQueue # angular.self-208d6d1….js?body=1:15970(anonymous function) # angular.self-208d6d1….js?body=1:15978Scope.$eval # angular.self-208d6d1….js?body=1:17230Scope.$digest # angular.self-208d6d1….js?body=1:17046Scope.$apply # angular.self-208d6d1….js?body=1:17338done # angular.self-208d6d1….js?body=1:11573completeRequest # angular.self-208d6d1….js?body=1:11779requestLoaded # angular.self-208d6d1….js?body=1:11712XMLHttpRequest.send (async)(anonymous function) # angular.self-208d6d1….js?body=1:11757sendReq # angular.self-208d6d1….js?body=1:11518serverRequest # angular.self-208d6d1….js?body=1:11228processQueue # angular.self-208d6d1….js?body=1:15962(anonymous function) # angular.self-208d6d1….js?body=1:15978Scope.$eval # angular.self-208d6d1….js?body=1:17230Scope.$digest # angular.self-208d6d1….js?body=1:17046Scope.$apply # angular.self-208d6d1….js?body=1:17338bootstrapApply # angular.self-208d6d1….js?body=1:1750invoke # angular.self-208d6d1….js?body=1:4666doBootstrap # angular.self-208d6d1….js?body=1:1748bootstrap # angular.self-208d6d1….js?body=1:1768angularInit # angular.self-208d6d1….js?body=1:1653(anonymous function) # angular.self-208d6d1….js?body=1:30864fire # jquery.self-4e23968….js?body=1:3188self.fireWith # jquery.self-4e23968….js?body=1:3318jQuery.extend.ready # jquery.self-4e23968….js?body=1:3537completed # jquery.self-4e23968….js?body=1:3553
I don't know why a get request is being made to that URL. Any thoughts? Thank you!
The templateUrl of the timerInfo should be absolute, like this:
templateUrl: '/app/assets/javascripts/timer.html'
This way you're referencing from the root directory down to the desired file.
Ah, I was able to figure it out. Apparently, the Angular-Rails gem looks for templates to load in an app/templates directory. I just had to make that directory and put the template there.

Hard to trace real root cause under chrome dev tools

I've changed dozens of controllers, services, and html
And got an Unknown provider exception
Unknown provider: bankServiceProvider <- bankService <- BanksBaseCtrl
However, it didn't tell me this is due to which file.
The debug message on chrome console is helpless.
Is there any useful way to trace the buggy source file?
Thanks so much
angular.js:12477 Error: [$injector:unpr] Unknown provider: bankServiceProvider <- bankService <- BanksBaseCtrl
http://errors.angularjs.org/1.4.7/$injector/unpr?p0=bankServiceProvider%20%3C-%20bankService%20%3C-%20BanksBaseCtrl
at http://dev.co/libs/angular/angular/angular.js:68:12
at http://dev.co/libs/angular/angular/angular.js:4289:19
at Object.getService [as get] (http://dev.co/libs/angular/angular/angular.js:4437:39)
at http://dev.co/libs/angular/angular/angular.js:4294:45
at getService (http://dev.co/libs/angular/angular/angular.js:4437:39)
at Object.invoke (http://dev.co/libs/angular/angular/angular.js:4469:13)
at extend.instance (http://dev.co/libs/angular/angular/angular.js:9136:34)
at nodeLinkFn (http://dev.co/libs/angular/angular/angular.js:8248:36)
at compositeLinkFn (http://dev.co/libs/angular/angular/angular.js:7680:13)
at publicLinkFn (http://dev.co/libs/angular/angular/angular.js:7555:30) <div ui-view="" class="fade-in ng-scope">

It means you injected bankService into BanksBaseCtrl and angular couldn't resolve bankService.
Verify that you properly registered bankService (with that exact name) with the angular module:
angular.module('yourapp').service('bankService',function(){...});
or, alternatively, correct the injected service name within BanksBaseCtrl.

Error after using ngAnimate: TypeError: Cannot read property 'ng-scope' and 'col-sm-6' of undefined

I've added angular animation to my project. I only placed this to my index.html and app.js. Is there something I've missed? Thanks in advance
Index.html
<script src="http://code.angularjs.org/1.2.1/angular-animate.js"></script>
App.js
angular.module('portfolio', ['ui.bootstrap','ui.router', 'ngAnimate', 'service', 'ctrl'])
Console: Error
TypeError: Cannot read property 'ng-scope' of undefined
at lookup (angular-animate.js:294)
at performAnimation (angular-animate.js:563)
at angular-animate.js:358
at m.$digest (angular.js:16215)
at m.$apply (angular.js:16429)
at g (angular.js:10823)
at t (angular.js:11021)
at XMLHttpRequest.w.onload (angular.js:10962)(anonymous function) # angular.js:12793(anonymous function) # angular.js:9526m.$digest # angular.js:16217m.$apply # angular.js:16429g # angular.js:10823t # angular.js:11021w.onload # angular.js:10962
angular.js:12793
TypeError: Cannot read property 'col-sm-6' of undefined
at lookup (angular-animate.js:294)
at performAnimation (angular-animate.js:563)
at angular-animate.js:358
at m.$digest (angular.js:16215)
at m.$apply (angular.js:16429)
at g (angular.js:10823)
at t (angular.js:11021)
at XMLHttpRequest.w.onload (angular.js:10962)
This question seems very similar to this post AngularJS: $$selectors undefined when trying to use ngAnimate
The suggestion seems to point to a mismatch between angular and angular animate module
I had the same error and the solution was the http. So i upgraded the version and i used this link instead. I hope that helps
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular-animate.js"></script>

how to remove given error " Failed to instantiate module app due to:"

I am trying to make simple app in angular js .I includes js files in my project. But I am getting this error
**Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module app.stationselect due to:
TypeError: $urlRouterProvider.state is not a function
at routeChange (http://localhost:63342/www/js/stationselect/router.js:9:28)
at Object.invoke (http://localhost:63342/www/lib/ionic.bundle.js:12877:17)
at runInvokeQueue (http://localhost:63342/www/lib/ionic.bundle.js:12783:35)
at http://localhost:63342/www/lib/ionic.bundle.js:12792:11
at forEach (http://localhost:63342/www/lib/ionic.bundle.js:9015:20)
at loadModules (http://localhost:63342/www/lib/ionic.bundle.js:12773:5)
at http://localhost:63342/www/lib/ionic.bundle.js:12790:40
at forEach (http://localhost:63342/www/lib/ionic.bundle.js:9015:20)
at loadModules (http://localhost:63342/www/lib/ionic.bundle.js:12773:5)
at createInjector (http://localhost:63342/www/lib/ionic.bundle.js:12699:11)
http://errors.angularjs.org/1.3.13/$injector/modulerr?p0=app.stationselect&…(http%3A%2F%2Flocalhost%3A63342%2Fwww%2Flib%2Fionic.bundle.js%3A12699%3A11)
at REGEX_STRING_REGEXP (http://localhost:63342/www/lib/ionic.bundle.js:8755:12)
at http://localhost:63342/www/lib/ionic.bundle.js:12812:15
at forEach (http://localhost:63342/www/lib/ionic.bundle.js:9015:20)
at loadModules (http://localhost:63342/www/lib/ionic.bundle.js:12773:5)
at http://localhost:63342/www/lib/ionic.bundle.js:12790:40
at forEach (http://localhost:63342/www/lib/ionic.bundle.js:9015:20)
at loadModules (http://localhost:63342/www/lib/ionic.bundle.js:12773:5)
at createInjector (http://localhost:63342/www/lib/ionic.bundle.js:12699:11)
at doBootstrap (http://localhost:63342/www/lib/ionic.bundle.js:10137:20)
at bootstrap (http://localhost:63342/www/lib/ionic.bundle.js:10158:12)
http://errors.angularjs.org/1.3.13/$injector/modulerr?p0=app&p1=Error%3A%20…(http%3A%2F%2Flocalhost%3A63342%2Fwww%2Flib%2Fionic.bundle.js%3A10158%3A12)**
actually I check
when I remove one script from index.html it remove that error
script is this
<script src="js/stationselect/router.js"></script>
But when I include again it show again given error
I don't what thing I write wrong ? Actually in my app there is some directory so I am not able to use fiddle .I will share my code with you
https://dl.dropbox.com/s/l2dtrxmnsurccxt/www.zip?dl=0
here is my script code
(function(){
'use strict'
angular.module('app.stationselect').config(routeChange);
routeChange.$inject=['$stateProvider','$urlRouterProvider'];
function routeChange($stateProvider, $urlRouterProvider){
$urlRouterProvider.state('selectstation',{
url:'/selectstation',
controller:'selectStation',
templateUrl:'js/stationselect/partial/home.html'
})
$urlRouterProvider.otherwise('/selectstation');
}
})();
Looks like you should have
angular.module('app.stationselect', ['ui.router']).config(...)
if you're creating the app.stationselect module there.
When you only provide one argument to angular.module() (ie, the module name), Angular attempts to retrieve it from the module registry.
If you've already created your module elsewhere, ignore the above.
Also, .state is a method of $stateProvider, not $urlRouterProvider. This is what's causing the error message.

Resources