我带着怀疑得到了数据,显示了这些数据,然后调用了函数,但是函数说“ null”。对不起我的英语不好。谢谢。
this.service.prepareNewVersion().subscribe(data2 => {
console.log("data2 ", data2);
this.service.myBlockPeriod = data2;
console.log(" prepareNewVersion ", this.service.myBlockPeriod);
});
console.log(" before ", this.service.myBlockPeriod);
this.showYearsExec();
private showYearsExec() {
console.log("showYearsExec", this.service.myBlockPeriod);
let list: Array<string> = this.service.myBlockPeriod;
if (list !== null) {
list.forEach(element => {
this.arrayYears.push(element.substring(0, 4));
});
// Se eliminan los años repetidos.
let unique = this.arrayYears.filter(function (elem, index, self) {
return index === self.indexOf(elem);
})
// Combo de años de la vista.
this.arrayYears = unique;
// Se añaden al servicio
this.service.yearsInExec = unique;
}
}
答案 0 :(得分:3)
您的代码是异步执行的,到执行“ this.showYearsExec()”时,尚未解决“ prepareNewVersion()”方法的承诺,因此结果为空。
执行“ this.showYearsExec();”在这样的订阅方法中:
this.service.prepareNewVersion().subscribe(data2 => {
console.log("data2 ", data2);
this.service.myBlockPeriod = data2;
console.log(" prepareNewVersion ", this.service.myBlockPeriod);
console.log(" before ", this.service.myBlockPeriod);
this.showYearsExec();
});