角种子范围问题

时间:2015-09-08 19:18:20

标签: javascript angularjs scope angular-seed

我目前正在尝试创建一个在每个routeProvider之前运行的帖子。目前我收到的问题是$ http未定义,我不知道为什么。我目前正在尝试将$ http传递给该函数,并且根据phpstorm $ http是' undefined'并且不知道为什么。我在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';
    });
}

浏览器显示:

enter image description here

这是错误信息。

  

[$ injector:modulerr]由于以下原因无法实例化模块myApp:错误:[$ injector:unpr]未知提供者:$ http http://errors.angularjs.org/1.4.5/ $ injector / unpr?p0 =%24http at匿名函数({{ 3)})atService(http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4284:13)在runInvokeQueue(http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4432:11)atman函数(http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4461:9)atEach(http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4379:11)处调用(http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4388:11)在loadModules(http://localhost:90/angular-seed/app/bower_components/angular/angular.js:336:11)处的doBootstrap(http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4369:5)处的createInjector(http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4294:3)处于引导程序(http://localhost:90/angular-seed/app/bower_components/angular/angular.js:1655:5http://localhost:90/angular-seed/app/bower_components/angular/angular.js:1676:5 $ injector / modulerr?p0 = myApp&amp; P1 =错误%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%20原子%20Anonymous%20function%20(HTTP%3A%2F%2Flocalhost%3A90%2Fangular种子%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4284%3A13)%0A%20 %20%20原子%20getService%20(HTTP%3A%2F%2Flocalhost%3A90%2Fangular种子%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4432%3A11)%0A%20%20%20原子%20invoke%20( HTTP%3A%2F%2Flocalhost%3A90%2Fangular种子%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4461%3A9)%0A%20%20%20原子%20R unInvokeQueue%20(HTTP%3A%2F%2Flocalhost%3A90%2Fangular种子%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4379%3A11)%0A%20%20%20原子%20Anonymous%20function%20(HTTP%3A %2F%2Flocalhost%3A90%2Fangular种子%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4388%3A11)%0A%20%20%20原子%20forEach%20(HTTP%3A%2F%2Flocalhost%3A90%2Fangular-种子%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A336%3A11)%0A%20%20%20个原子%20loadModules%20(HTTP%3A%2F%2Flocalhost%3A90%2Fangular种子%2Fapp%2Fbower_components%2Fangular%2Fangular的.js%3A4369%3A5)%0A%20%20%20原子%20createInjector%20(HTTP%3A%2F%2Flocalhost%3A90%2Fangular种子%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4294%3A3)%0A %20%20%20原子%20doBootstrap%20(HTTP%3A%2F%2Flocalhost%3A90%2Fangular种子%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A1655%3A5)%0A%20%20%20原子%20bootstrap% 20(HTTP%3A%2F%2Flocalhost%3A90%2Fangular种子%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A1676%3A5)

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

1 个答案:

答案 0 :(得分: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'});
}])