简体   繁体   English

TypeError:无法读取未定义的属性“ $ cookies”

[英]TypeError: Cannot read property '$cookies' of undefined

I'm trying to save a response from server to cookies, and here is how I'm trying to do 我正在尝试将服务器的响应保存到Cookie,这就是我试图做的

this.$http({
            method: 'POST',
            url: SOME_URL,
            data: SOME_DATA,
            headers: {
                'Authorization': this.basicAuthorization
            }


        }).success(function(data){
            //console.log(data);
            this.$cookies.putObject('userData', data);
        }).error(function(data){

        });
    }

I also provided: 我还提供了:

    angular.module('mymodule', [
            ....,
            'ngCookies'
        ])
    // ........
    LoginHandler.$inject = ['$http', '$cookies'];
    // ........
    constructor($http, $cookies){ ... }

What is causing the error? 是什么导致错误? I'm using angularjs 1.4.0 我正在使用angularjs 1.4.0

UPDATE 更新

I have already found out that this is not working as expected in .success() etc. So I had to store this in some other variable. 我已经发现this无法在.success()等中正常工作。因此我不得不将其存储在其他变量中。

Try like this, 这样尝试

var self = this;
this.$http({
            method: 'POST',
            url: SOME_URL,
            data: SOME_DATA,
            headers: {
                'Authorization': this.basicAuthorization
            }

        }).success(function(data){
            //console.log(data);
            self.$cookies.putObject('userData', data);
        }).error(function(data){

        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM