angularjs http interceptor class (ES6) verliert die Bindung an 'this'
Ich erstelle eine AngularJS-App mit ES6-Klassen und Traceur-Transpiling für ES5 im AMD-Format.
in meinem Modul importiere ich die Interceptor-Klasse und registriere sie als Dienst und registriere diesen Dienst dann mit den $ httpProvider.interceptors in module.config:
var commonModule = angular.module(moduleName, [constants.name]);
import authenticationInterceptor from './authentication/authentication.interceptor';
commonModule.service('authenticationInterceptor', authenticationInterceptor);
commonModule.config( $httpProvider => {
$httpProvider.interceptors.push('authenticationInterceptor');
});
Meine Interceptor-Klasse injiziert beide $ q und die $ window services speichert sie für die spätere Verwendung im Konstruktor. Ich habe diesen Teil mit dem Debugger verfolgt und die Injection läuft korrekt:
'use strict';
/*jshint esnext: true */
var authenticationInterceptor = class AuthenticationInterceptor {
/* ngInject */
constructor($q, $window) {
this.$q = $q;
this.$window = $window;
}
responseError(rejection) {
var authToken = rejection.config.headers.Authorization;
if (rejection.status === 401 && !authToken) {
let authentication_url = rejection.data.errors[0].data.authenticationUrl;
this.$window.location.replace(authentication_url);
return this.$q.defer(rejection);
}
return this.$q.reject(rejections);
}
}
authenticationInterceptor.$inject = ['$q', '$window'];
export default authenticationInterceptor;
Wenn ich eine Anfrage stelle, die mit einem @ beantwortet wi 401 der Interceptor wird entsprechend ausgelöst, aber in der 'responseError'-Methode zeigt die' this'-Variable auf das Fensterobjekt und nicht auf meinen Interceptor, daher habe ich keinen Zugriff auf this. $ q oder this. $ window.
Ich kann nicht herausfinden warum? Irgendwelche Ideen