简体   繁体   English

Angular 5从父组件中删除子组件

[英]Angular 5 remove child component from parent component

I want to remove one of the child components from its parent when the user clicks on the close button present in the child component. 当用户单击子组件中存在的关闭按钮时,我想从其父组件中删除一个子组件。 I have 3 components in my parent component. 我的父级组件中有3个组件。

<app-header></app-header>
<app-map></app-map>
<app-rules *ngIf="selectedId"></app-rules>

I tried to update the value of selectedId to undefined on click of a close button inside . 我试图在单击内部的关闭按钮时将selectedId的值更新为undefined。 but it is not taking the updated value and closing the component. 但它不会采用更新的值并关闭组件。

Your problem might be that *ngIf expects a boolean state. 您的问题可能是* ngIf期望为布尔状态。 undefined is an invalid state when you validate a number. 验证数字时,undefined是无效状态。 You have to implement a comparison then eg 你必须实现一个比较然后

// selectedId is a number
<app-rules *ngIf="selectedId > 0"></app-rules>

or 要么

// selectedId is a string
<app-rules *ngIf="selectedId !== undefined"></app-rules>

// in case of a string this is also possible
<app-rules *ngIf="selectedId"></app-rules>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM