Angular 函数不返回角度值?

Angular 函数不返回角度值?,angular,Angular,我试图在ngOnInit中获取第二个函数返回值,但它给出了未定义的值。如果我打印它的打印。我不知道我在哪里犯的错误 ngOnInit() { this.userService.displayEvents(this.user_id).subscribe(data => { this.eventArray = []; if (data.status == 'success') { data.result.forEach(obj => { let item =

我试图在ngOnInit中获取第二个函数返回值,但它给出了未定义的值。如果我打印它的打印。我不知道我在哪里犯的错误

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
不返回任何内容。调用this.userService.getEventCategory().subscribe((数据)=>{…})创建一个
订阅,但您不返回它

尝试以下mods:

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: '',
                    category: obj.category,
                    start_date: obj.start_date,
                    status: obj.status,
                };
                this.eventArray.push(item);
                this.getSvgImage(item, obj.category, obj.color_code),
            });
            this.displayEvents = this.eventArray;
            console.log(this.displayEvents);
        }
    });
}

getSvgImage(item, 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);
                }
            });
        }
        item.image = SvgImage;
    });
}

getSvgImage将作为第一个参数获取item对象,一旦订阅完成,它将更新image属性。

是的,它不会返回任何内容。如果您需要,那么一定要返回一些内容!:-你敢肯定getSvgImage函数中的SvgImage返回undefined,或者是未定义项的image属性,因为当item.image获取值时getEventCategory()尚未返回?我肯定,我正在控制台中打印SvgImage。它来了,但没有从函数返回相同的值。