【发布时间】:2020-02-13 12:52:15
【问题描述】:
我试图在 ngOnInit 中获取第二个函数的返回值,但它给出了未定义的值。如果我打印 SvgImage 它的打印。我不知道我在哪里做错了。
ngOnInit() {
this.userService.displayEvents(this.user_id).subscribe(data => {
this.eventArray = [];
if (data.status == 'success') {
data.result.forEach(obj => {
let item = {
id: obj.id,
user_id: obj.user_id,
name: obj.name,
image: this.getSvgImage(obj.category, obj.color_code),
category: obj.category,
start_date: obj.start_date,
status: obj.status,
};
this.eventArray.push(item);
});
this.displayEvents = this.eventArray;
console.log(this.displayEvents);
}
});
}
getSvgImage(categoryID: any, colorCode: any) {
this.userService.getEventCategory().subscribe((data) => {
let SvgImage: any = "";
if (data.status == "success") {
data.result.forEach(obj => {
if (obj.id == categoryID) {
let color = colorCode.replace("#", "");
let SvgImageReplace = obj.image.split('#').pop().split(';')[0];
SvgImage = obj.image.replace(SvgImageReplace, color);
SvgImage = this.sanitized.bypassSecurityTrustHtml(SvgImage);
}
});
}
return SvgImage;
});
}
【问题讨论】:
-
你确定getSvgImage函数中的SvgImage返回undefined还是item的image属性是undefined,因为getEventCategory()在item.image取值时没有返回?
-
我确定,我正在控制台中打印 SvgImage。它来了,但函数没有返回相同的值。
标签: angular