【问题标题】:Typescript: data not getting assigned to class variable打字稿:数据未分配给类变量
【发布时间】:2017-05-20 07:30:04
【问题描述】:

我正在使用 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”行看到一个错误。 错误是-TypeError:无法设置未定义的属性“数据”。 我没有得到这个问题的解决方案。有人可以帮忙吗?

【问题讨论】:

    标签: angularjs http typescript this


    【解决方案1】:

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

    尝试改变:

    .then(function successCallback(response)
    

    .then((response) => 
    

    【讨论】:

      猜你喜欢
      • 2018-05-06
      • 2019-11-16
      • 2017-01-18
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      相关资源
      最近更新 更多