我正在使用 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/