【问题标题】:Function not returning Value in angular?函数不以角度返回值?
【发布时间】: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


【解决方案1】:

尝试以下模组:

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 将作为第一个参数获取项目对象,一旦订阅完成,它将更新图像属性。

【讨论】:

    【解决方案2】:

    函数getSvgImage 不返回任何内容。调用this.userService.getEventCategory().subscribe((data) => { ... }) 创建了一个Subscription,但您没有返回它。

    【讨论】:

    • 是的,它没有返回任何东西。
    • 好吧,那就返回一些东西吧,如果那是你想要的! :-D
    猜你喜欢
    • 2018-01-22
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多