SORU
18 Aralık 2013, ÇARŞAMBA


AngularJS: HTTP avcı (Döngüsel bağımlılık)içine Enjekte servisi

Benim AngularJS için HTTP avcı kimlik doğrulama işlemek için uygulama yazmaya çalışıyorum.

Bu kod çalışır, ancak el ile Açısal bunu otomatik olarak işlemek için olduğunu sanıyordum beri hizmet enjekte endişeliyim:

    app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push(function ($location, $injector) {
        return {
            'request': function (config) {
                //injected manually to get around circular dependency problem.
                var AuthService = $injector.get('AuthService');
                console.log(AuthService);
                console.log('in request interceptor');
                if (!AuthService.isAuthenticated() && $location.path != '/login') {
                    console.log('user is not logged in.');
                    $location.path('/login');
                }
                return config;
            }
        };
    })
}]);

Dışarı yapmaya başladı, ama döngüsel bağımlılık sorunları vardı:

    app.config(function ($provide, $httpProvider) {
    $provide.factory('HttpInterceptor', function ($q, $location, AuthService) {
        return {
            'request': function (config) {
                console.log('in request interceptor.');
                if (!AuthService.isAuthenticated() && $location.path != '/login') {
                    console.log('user is not logged in.');
                    $location.path('/login');
                }
                return config;
            }
        };
    });

    $httpProvider.interceptors.push('HttpInterceptor');
});

Endişeliyim Açısal Dokümanlar section on $http bağımlılıkları enjekte almak için bir yol gösteren başka nedeni "normal yoldan" Http içine uçağı. Onların kuralları altında snippet"":. Önleyicilerin

// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
  return {
    // optional method
    'request': function(config) {
      // do something on success
      return config || $q.when(config);
    },

    // optional method
   'requestError': function(rejection) {
      // do something on error
      if (canRecover(rejection)) {
        return responseOrNewPromise
      }
      return $q.reject(rejection);
    },



    // optional method
    'response': function(response) {
      // do something on success
      return response || $q.when(response);
    },

    // optional method
   'responseError': function(rejection) {
      // do something on error
      if (canRecover(rejection)) {
        return responseOrNewPromise
      }
      return $q.reject(rejection);
    };
  }
});

$httpProvider.interceptors.push('myHttpInterceptor');

Yukarıdaki kodu nereye gitmeli?

Benim sorum bu yapıyor hakkında gitmek için doğru yolu nedir?

Teşekkürler, benim sorum yeterince açık oldu umarım.

CEVAP
7 ŞUBAT 2014, Cuma


$Http ve AuthService arasında döngüsel bir bağımlılık var.

$injector servis kullanarak yaptığınız işe AuthService $http bağımlılık geciktirerek tavuk ve yumurta sorunu çözmek olacak.

Yaptığın şey aslında bunu yapmanın en kolay yolu olduğuna inanıyorum.

Ayrıca bunu alabilirsin:

  • Interceptor sonra (yani config() bir blok yerine run() bir blok yapıyor zaten işi olabilir) kaydediliyor. Ama $http aradı bile olmadı garanti edebilir?
  • "Enjekte AuthService.setHttp() çağırarak ınterceptor kayıt olduğunuzda" $http AuthService içine elle falan.
  • ...

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • boogie2988

    boogie2988

    6 NİSAN 2006
  • justintimberlakeVEVO

    justintimber

    2 EYLÜL 2009
  • TV nEW

    TV nEW

    25 AĞUSTOS 2012