发出更新事件时的项目值为null

发布于 2025-02-03 05:55:11 字数 708 浏览 2 评论 0 原文

onCardClicked(item: BudgetItem) {
const dialogRef = this.dialog.open(EditItemModalComponent, {
  width: '580px',
  data: item
})

console.log(item)

dialogRef.afterClosed().subscribe(result => {
  // check if result has a value
  if (result) {
    console.log(item)

    this.update.emit(
      {
        old: item,
        new: result
      }
    );
  }

})}

当我尝试读取subscribe()内部项目的值时,它似乎是空的。据我所知,箭头功能应该能够使用本地范围中的值。因此,应该读取项目值。

因此,Angular无法计算总预算,并且在主页组件中显示不正确的总数。

如果有人有任何想法,请提供帮助!

请在Stackblitz中找到完整的代码

onCardClicked(item: BudgetItem) {
const dialogRef = this.dialog.open(EditItemModalComponent, {
  width: '580px',
  data: item
})

console.log(item)

dialogRef.afterClosed().subscribe(result => {
  // check if result has a value
  if (result) {
    console.log(item)

    this.update.emit(
      {
        old: item,
        new: result
      }
    );
  }

})}

When I try to read the value of item inside of subscribe() , it appears to be null. As per my knowledge as arrow function should be able to use the value from the local scope.. hence item value should be read.

Hence as a result , angular cannot calculate the total budget and shows incorrect total in the main page component.

If anyone has any idea, please help!

Please find the full code in stackblitz
https://stackblitz.com/edit/github-tvrqgd?file=src/app/budget-item-list/budget-item-list.component.ts

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以笙箫默 2025-02-10 05:55:11

您将在订阅回调中收到的结果参数中有数据。箭头功能没有本地范围。如果您仍然想使用项目而不是结果,则必须将其转换为非箭头功能并像以下内容一样绑定: -

 onCardClicked(item: BudgetItem) {
    const dialogRef = this.dialog.open(EditItemModalComponent, {
      width: '580px',
      data: item,
    });

    console.log(item);
    var closeCallback = function (result) {
      // check if result has a value
      console.log('result');
      console.log(result);
      if (result) {
        console.log(item);

        this.update.emit({
          old: item,
          new: result,
        });
      }
    }.bind(this);
    dialogRef.afterClosed().subscribe(closeCallback);
  }

工作stackblitz: - https://stackblitz.com/edit/edit/github-tvrqgd?file=src/app/budget-/budget-gudget-gudget-gudget-gudget-gudget-gudget-app-- item-list/budgem-item-list.component.ts

You will have data in result parameter you are receiving in subscribe callback. Arrow function don't have local scope. If you still want to use items instead of result you have to convert it into non arrow function and bind it like below :-

 onCardClicked(item: BudgetItem) {
    const dialogRef = this.dialog.open(EditItemModalComponent, {
      width: '580px',
      data: item,
    });

    console.log(item);
    var closeCallback = function (result) {
      // check if result has a value
      console.log('result');
      console.log(result);
      if (result) {
        console.log(item);

        this.update.emit({
          old: item,
          new: result,
        });
      }
    }.bind(this);
    dialogRef.afterClosed().subscribe(closeCallback);
  }

Working Stackblitz :- https://stackblitz.com/edit/github-tvrqgd?file=src/app/budget-item-list/budget-item-list.component.ts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文