2017-06-26 6 views
0

ここに私のサービスは、取扱説明書、ブートストラップ前に$ HTTPを必要とするサービスを注入AngularJSは

(function(angular, module){ 

    module.provider('auth', ['$http', function($http){ 
     this.$get = function(){ 
      return { 
       user: function(){ 
        return {then: function(){}} 
       } 
      } 
     } 
    }]); 

})(angular, angular.module('auth', [])); 

私はアプリのブートストラップ前にユーザーに確認したいと思いますので、私がやっている:

angular.element(document).ready(function() { 
    var auth = angular.injector(['auth', 'ng']).get('auth'); 

    auth.user().then(function(response){ 
     angular.bootstrap(document, ['app']); 
    }); 
}); 

をが、 $httpサービスは解決されていません。実際には認証サービスから依存関係を削除してもエラーは発生しません。何が間違っているのですか?

答えて

3

は、なぜあなたはこのよう

// create an injector 
var $injector = angular.injector(['ng']); 

$injector.invoke(function($http) { 
    $http.get(url).then(function() { 
    angular.bootstrap(document, ['app']); 
    }); 
}); 

あなたの方法を試していけない:

(function(angular, module){ 

    angular.module('authApp', []).provider('auth', function(){ 
     this.$get = ['$http', function($http) { 
      return { 
       user: function(){ 
        return 123; 
       } 
      } 
     }] 
    }); 

})(angular, angular.module('authApp', [])); 

var $injector = angular.injector(['ng', 'authApp']); 

$injector.invoke(function(auth) { 
    console.log(auth); 
    auth.user().then(function(response){ 
    angular.bootstrap(document, ['app']); 
    }); 
}); 
+0

私は別のサービスで認証をラップしたいので。 – brazorf

+0

OK私は私の答えを編集しました、それはあなたにもっと今願って願って]; –

+0

うんうん;)ありがとう。 – brazorf