【问题标题】:Angularjs Using $http and $cookies in ServiceAngularjs 在服务中使用 $http 和 $cookies
【发布时间】:2018-06-24 01:08:05
【问题描述】:

当我尝试使用 angular-cookie 和 $http 时出现错误: TypeError: 无法读取未定义的属性 'then'

我是否以错误的方式注入它?没有 ng-cookies 一切正常!

谢谢

static $inject = ['$rootScope','$http', '$cookies', '$q','$cookieStore','$window'];

constructor(public $scope: ng.IScope, public $window: ng.IWindowService, public $http: ng.IHttpService, private $q: ng.IQService, public $cookieStore: ng.cookies.ICookieStoreService, public $cookies: ng.cookies.ICookiesService) {
}

loginUserOnApi (userData: any) {

    let deferred = this.$q.defer();
    let parameters = {
        username: userData.title,
        password: userData.password
    };

    let config = {
      params: parameters,
      withCredentials: true,
      headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
    };
    let self  = this;

    this.$http.get(this.loginApiUrl , config).then(response => {
        self.status = response.data['ack'];
        self.$cookies.putObject('new-acc-var',response);
        deferred.resolve(response.data);

    }).catch( reason => {
        deferred.reject(reason);
    });

    return deferred.promise;

   }

错误:

index.js:67374 TypeError: Cannot read property 'then' of undefined
at LoginService.loginUserOnApi (index.js:9794)
at LoginUserController.loginUserOverService (index.js:8911)
at fn (eval at compile (index.js:68225), <anonymous>:4:202)
at callback (index.js:80046)
at Scope.$eval (index.js:71116)
at Scope.$apply (index.js:71215)
at HTMLFormElement.<anonymous> (index.js:80051)
at defaultHandlerWrapper (index.js:56368)
at HTMLFormElement.eventHandler (index.js:56356)

【问题讨论】:

    标签: angularjs http cookies


    【解决方案1】:
    static $inject = ['$rootScope','$http', '$cookies', '$q','$cookieStore','$window'];
    
    constructor(public $scope: ng.IScope, public $window: ng.IWindowService, public $http: ng.IHttpService, private $q: ng.IQService, public $cookieStore: ng.cookies.ICookieStoreService, public $cookies: ng.cookies.ICookiesService) {
    }
    

    检查 - 顺序不正确。在 $inject http 中是第 2 位,在函数中 - 第 3 位。

    【讨论】:

      猜你喜欢
      • 2012-10-03
      • 2013-03-29
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 2012-08-18
      • 1970-01-01
      相关资源
      最近更新 更多