Angular 12 将数据传递给 ngFor 中的子级

发布于 2025-01-09 12:58:10 字数 1159 浏览 3 评论 0原文

我在将数据传递给放在 PrimeNg TabPanel 中的 ngFor 中的子级时遇到问题。 父组件类似于:

<p-tabPanel header="{{tab.title}}" *ngFor="let tab of tabs; let i = index" [closable]="true" [selected]="active[i+1]">
   <app-edit-tab [profile]="profiles[i]" (profileModifiedEvent)="profileModifiedEvent($event, i)"></app-edit-tab>
</p-tabPanel>

子组件是一个编辑选项卡,包含:

@Component({
    selector: 'app-edit-tab',
    templateUrl: './edit-tab.component.html'        
})
export class EditTabComponent implements OnInit {
    @Input() profile: Profile;
    backupProfile: Profile;
    @Output() profileModifiedEvent: new EvenEmitter<Profile>();

    ngOnInit() {
        this.backupProfile = Object.assign({}, profile);
    }

    editProfile() {
        this.profileModifiedEvent.emit(this.profile);
    }
}

我为重置编辑字段制作备份副本,并使用输入配置文件对象作为模板中的模型。 如果我打开 2 个选项卡,我会在 onInit 中检查备份副本是否正确:第一个选项卡包含传递的第一个配置文件的副本,称之为 A,第二个选项卡包含传递的第二个配置文件的副本,称之为 B。 问题是,在第二个选项卡中,当调用 editProfile() 时,配置文件对象不是 B,而是 A!就好像第二个选项卡具有配置文件 A 的内容,但我在 onInit 中检查到配置文件是 B! 有办法解决这个问题吗?每个选项卡都需要有一个输入配置文件对象的副本。

编辑: 也许真正的问题是:将从数组获取的参数传递给子组件的最佳方法是什么?

I'ma having a trouble passing data to child in ngFor put in a PrimeNg TabPanel.
The parent is like:

<p-tabPanel header="{{tab.title}}" *ngFor="let tab of tabs; let i = index" [closable]="true" [selected]="active[i+1]">
   <app-edit-tab [profile]="profiles[i]" (profileModifiedEvent)="profileModifiedEvent($event, i)"></app-edit-tab>
</p-tabPanel>

Child component is an edit tab and contains:

@Component({
    selector: 'app-edit-tab',
    templateUrl: './edit-tab.component.html'        
})
export class EditTabComponent implements OnInit {
    @Input() profile: Profile;
    backupProfile: Profile;
    @Output() profileModifiedEvent: new EvenEmitter<Profile>();

    ngOnInit() {
        this.backupProfile = Object.assign({}, profile);
    }

    editProfile() {
        this.profileModifiedEvent.emit(this.profile);
    }
}

I make a backup copy for reset edited fields, and I use input profile object as model in template.
If I open 2 tabs, I check in onInit that backup copy is right: the first tab, contains a copy of the first profile passed, call it A, the second tab, contains a copy of the second profile passed, call it B.
The problem is that, in second tab, when editProfile() is called, the profile object isn't B, but A! It's like the second tab has the content of profile A but I checked in onInit that profile was B!
Is there a way to solve this problem? It's need to have a copy of the input Profile object for each tab.

EDIT:
Maybe the real question is: which is the best way to pass argument, taken from array, to child component?

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

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

发布评论

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

评论(1

梦过后 2025-01-16 12:58:10

如果我是你,我会尝试生命周期钩子 ngOnChanges()。尽管我不知道您所提供的确切用例/问题,但您的子组件有可能在第一次渲染时将 @Input() 值设置为某个值。然后,当父级中的值发生更改时,子级中的值不会自动更新,因为这就是 Angular 的工作原理。

使用 ngOnChanges() {} 检测更改并相应更新您的 @Input() 值!

If I were you I would try the lifecycle hook ngOnChanges(). Although I do not know your exact use case/problem from what you have provided, there is a chance that your child component is rendering the first time with the @Input() value set to a certain value. Then when the value changes in the parent it is not automatically updated in the child because this is how Angular works.

Use ngOnChanges() {} to detect the change and update your @Input() value accordingly!

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