0

我目前正在尝试创建一个在每个 routeProvider 之前运行的帖子。目前我收到一个 $http 未定义的问题,我不知道为什么。我目前正在尝试将 $http 传递给函数,并且根据 phpstorm $http 是“未定义”并且不知道为什么。我确实在 index.html 中的 validation.js 之前声明了 angular.js。

<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>   

我的 app.js 看起来像这样:

'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'});
}]);

组件/通用/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';
    });
}

浏览器显示:

在此处输入图像描述

这是错误消息。

[$injector:modulerr] 无法实例化模块 myApp 由于:错误:[$injector:unpr] 未知提供者:$http http://errors.angularjs.org/1.4.5/ $injector/unpr?p0=%24http在匿名函数 ( http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4284:13 ) 在 getService ( http://localhost:90/angular-seed/app/bower_components/angular /angular.js:4432:11 ) 在 runInvokeQueue ( http://localhost:90/angular 调用 ( http ://localhost:90/angular-seed/app/bower_components/angular/angular.js:4461:9 ) -seed/app/bower_components/angular/angular.js:4379:11 ) 在匿名函数 ( http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4388:11 ) 在 forEach (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:336:11 ) 在 loadModules ( http://localhost:90/angular-seed/app/bower_components/angular/angular.js :4369:5 ) 在 doBootstrap ( http ://localhost:90/angular-seed/app的 createInjector ( http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4294:3 ) /bower_components/angular/angular.js:1655:5)在引导程序(http://localhost:90/angular-seed/app/bower_components/angular/angular.js:1676:5http://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)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)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)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)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)

没有设置 $http 的任何帮助或指向正确的方向都会有所帮助。谢谢你。如果您需要任何其他信息,请告诉我,以便我更新问题。

4

1 回答 1

1

$http服务不能分阶段使用config,因为提供者可能还没有准备好。您可以在这里使用的是方法中的resolve属性$routeProvider.when()

'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'});
}])
于 2015-09-08T19:25:32.230 回答