gpt4 book ai didi

angular - Angular 形|当我使用2 ngIf时,如何读取子组件的属性?

转载 作者:行者123 更新时间:2023-12-03 08:22:51 25 4
gpt4 key购买 nike

例如)post_no = 1,点击EDIT按钮。

发生错误。

错误:无法读取未定义的属性“编辑”。

...
<app-editor *ngIf="!post_no" #editor_save></app-editor>
<app-editor *ngIf="post_no" #editor_edit></app-editor>
...
<div *ngIf="post_no; else notEdit">
<button class="post-save-btn" (click)="editor_edit.edit()">EDIT</button>
</div>
<ng-template #notEdit>
<button class="post-save-btn" (click)="editor_save.save()">SAVE</button>
</ng-template>
</div>

使用两个ngIf似乎会导致属性错误。 (editor_edit.edit())

有没有好的解决方案?

不使用两个ngIf的其他方法也可以。

最佳答案

使用隐藏属性,这将有助于保持DOM存在。

<app-editor [hidden]="post_no" #editor_save></app-editor>
<app-editor [hidden]="!post_no" #editor_edit></app-editor>
...
<div *ngIf="post_no; else notEdit">
<button class="post-save-btn" (click)="editor_edit.edit()">EDIT</button>
</div>
<ng-template #notEdit>
<button class="post-save-btn" (click)="editor_save.save()">SAVE</button>
</ng-template>
</div>

这是 Stackblitz演示。

关于angular - Angular 形|当我使用2 ngIf时,如何读取子组件的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52070729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com