angularjs - typescript :数据未分配给类变量

标签 angularjs http typescript this

我正在使用 http 调用从后端获取数据。当数据登录到浏览器控制台时,我可以看到从后端返回的数据。 但是数据没有分配给类变量。下面是我的代码- “使用严格”;

export class EditTranslationsController {
    static IID: string = "EditTranslationsController";
    static $inject: string[] = ["$http", "$scope", "$log"];

    private _welcome : string = "HELLO WORLD!";
    public data : any;
    constructor(private $http: angular.IHttpService, 
                private $log: angular.ILogService,
                private $scope: angular.IScope) {

       this.sendUpdatedData();
    }

    public sendUpdatedData = () => {
        this.$http({
                    method : 'POST',
                    url : '/test'
            }).then(function successCallback(response) {
                console.log(response.data);
                this.data = response.data;
            }, function errorCallback(response) {
                this.$log.error(response);
            });
    }
}

因此,如果我调试我的代码,我会在“this.data=response.data”行看到一个错误。 错误是类型错误:无法设置未定义的属性“数据”。 我没有得到这个问题的解决方案。有人可以帮忙吗?

最佳答案

我怀疑这是由于“this”在传递给“then”的函数中实际代表的内容。它没有引用类。

尝试改变:

.then(function successCallback(response)

.then((response) => 

关于angularjs - typescript :数据未分配给类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44083148/

相关文章:

http - 成功后如何调用函数

python - 如何以 'smarter' 的方式使用 python 下载文件?

angularjs - Angular $http 状态 (-1)

javascript - 如何在 Angular masonry 中访问 masonry 对象的事件

ruby - 为什么 Ruby 中的 curl 比命令行 curl 慢?

javascript - 如何确定要从 block 内调用的回调函数的范围?

javascript - Uncaught Error : Template parse errors: Angular 4

javascript - typescript :将方法添加到类的数组

javascript - 如何在 Angular 1.2 中使用 ng-animate?

html - 拒绝在帧中显示 'https://www.youtube.com/watch?v=oKZRsBjQJOs',因为它将 'X-Frame-Options' 设置为 'sameorigin'