我有以下场景......
// child-component.ts
@Input() attrOne: any;
@Input() attrTwo: any;
isVisible: boolean = true;
HideThis(){
this.isVisible = false;
}
和...
// child-component.html
<div *ngIf="isVisible">
<!-- nested content -->
<span (click)="HideThis()">Hide This</span>
</div>
然后...
// parent-component.html
// listData comes from server as is...
<ng-container *ngFor="let item of listData">
<child-component [attrOne]="item.propOne" [attrTwo]="item.propTwo"></child-component>
</ng-container>
一个子组件,我们称之为child-component,嵌入在parent-component中。我在父组件上使用 ng-for来使用嵌入的子组件列出数据数组...我需要能够在任何嵌入的子组件上执行(click)="HideThis()"。 .. 我的尝试(如上)隐藏了内容,但是当我单击 HideThis() 时,在父组件的 DOM 中留下了一个空白的子组件元素。我希望完全删除或避免列出相应的子组件。
我不能使用 listData[n].item.prop 之类的属性来进行 *ngIf 测试。listData 来自远程服务器。有没有办法避免使用类似的东西@Output() onHidden: EventEmitter<any> = new EventEmitter<any>();
我尝试过ng-template和ng-content无济于事。最好的方法是什么?