I am currently trying to create a post that runs before each routeProvider. Currently I am receiving an issue that $http is undefined and I don't know why. I am currently trying to pass $http to the function and according to phpstorm $http is 'undefined' and don't know why. I do declare angular.js before validation.js in the index.html.
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="components/universal/validation.js"></script>
And my app.js looks like this:
'use strict';
angular.module('myApp', [
'ngRoute',
'myApp.view1',
'myApp.view2',
'myApp.test',
'myApp.version'
]).
config(['$routeProvider','$http', function($routeProvider,$http) {
alert ('before call');
var temp = sessionValidation($http);
alert('temp : '+temp);
$routeProvider.otherwise({redirectTo: '/view1'});
}]);
Components/universal/validation.js
function sessionValidation($http) {
alert('before post');
return $http({
url: 'http://255.255.255.255/rip.dll/REST/SESSIONS/',
method: 'POST',
dataType:"json",
xhrFields :{"withCredentials" : true},
data: {'logintype':'1','host':'255.255.255.255','user':'Administrator','password':'1234','controlid':'ABC999'}
})
.success(function (data) {
return data.stats;
})
.error(function () {
return 'Error';
});
}
And the browser displays this:
And here is the error message.
[$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:unpr] Unknown provider: $http http://errors.angularjs.org/1.4.5/$injector/unpr?p0=%24http at Anonymous function (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4284:13) at getService (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4432:11) at invoke (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4461:9) at runInvokeQueue (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4379:11) at Anonymous function (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4388:11) at forEach (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:336:11) at loadModules (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4369:5) at createInjector (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4294:3) at doBootstrap (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:1655:5) at bootstrap (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:1676:5) http://errors.angularjs.org/1.4.5/$injector/modulerr?p0=myApp&p1=Error%3A%20%5B%24injector%3Aunpr%5D%20Unknown%20provider%3A%20%24http%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.4.5%2F%24injector%2Funpr%3Fp0%3D%2524http%0A%20%20%20at%20Anonymous%20function%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4284%3A13)%0A%20%20%20at%20getService%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4432%3A11)%0A%20%20%20at%20invoke%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4461%3A9)%0A%20%20%20at%20runInvokeQueue%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4379%3A11)%0A%20%20%20at%20Anonymous%20function%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4388%3A11)%0A%20%20%20at%20forEach%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A336%3A11)%0A%20%20%20at%20loadModules%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4369%3A5)%0A%20%20%20at%20createInjector%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4294%3A3)%0A%20%20%20at%20doBootstrap%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A1655%3A5)%0A%20%20%20at%20bootstrap%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A1676%3A5)
Any help or point in the right direction why $http isn't set would be helpful. Thank you. If you need any other info, please let me know so I can update question.
The $http
service can't be used in config
phase, as the provider might not be ready yet. What you can use here is the resolve
property in $routeProvider.when()
method:
'use strict';
angular.module('myApp', [
'ngRoute',
'myApp.view1',
'myApp.version'
]).
config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/view1', {
resolve: {
sessionValidation: function($http) {
return sessionValidation($http);
}
}
})
.otherwise({redirectTo: '/view1'});
}])