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 ) 呼び出し ( http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4461:9 ) で runInvokeQueue ( 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 ) で 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 ) createInjector ( http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4294:3 ) で doBootstrap ( http://localhost:90/angular-seed/app ) で/bower_components/angular/angular.js:1655:5 ) ブートストラップ ( http://localhost:90/angular-seed/app/bower_components/angular/angular.js:1676:5 ) http://errors.angularjs で。組織/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 に答える