此代码是一个使用angularjs和typescript的ASP.net应用程序
在html文件中,有一个ng-repeat(用于填充表)正在观察变量this.csvData(由private csvData = []
定义的实例变量)并且表生成完美。但是,当我在updateDataDict函数中尝试该变量的console.log时,控制台返回[]。我不确定为什么......
public csvData = [];
private csvDataDict = {};
private getCSVdata() {
var locationType = document.getElementById('dropdownButton').innerText.toLowerCase();
console.log("Inside CSV data");
let dataArray = [];
this.$http.get(this.csvLocations[locationType]).then(function (res) {
var result = res.data.toString().split(/\r\n|\n/);
for (let i = 0; i < result.length; i++) {
let [sourceURL, redirectURL] = result[i].split(',');
dataArray.push({
sourceURL: sourceURL,
redirectURL: redirectURL
})
}
});
this.csvData = dataArray;
this.updateDataDict()
}
private updateDataDict() {
console.log(this.csvData); // this line is screwing up.....
}
答案 0 :(得分:3)
此
var result = res.data.toString().split(/\r\n|\n/);
for (let i = 0; i < result.length; i++) {
let [sourceURL, redirectURL] = result[i].split(',');
dataArray.push({
sourceURL: sourceURL,
redirectURL: redirectURL
})
}
正在执行
之后执行this.csvData = dataArray;
this.updateDataDict()
因为它是一个异步调用,所以你在实际填充之前检查数组。