首页 > 解决方案 > 如何在父组件Angular 7中获取在HTML中处于活动状态的子组件名称

问题描述

我想获取父组件中存在的子组件名称。单击父组件中可用的按钮时,我想知道当前 HTML 中的活动子组件。

父组件 ts 文件

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  active = true;
  anyFunction() {
    //changing tab status for child
  }
}

父组件 html 文件

<div>
    <button (click)="anyFunction()"></button>
    <app-child1 *ngIf="active"></app-child1>
    <app-child2 *ngIf="!active"></app-child2>
 </div>

如果我正在显示 child1 组件,那么当我单击我想知道的按钮时,我想获取 Child1Component。应显示 HTML 中的活动项。

标签: angulartypescript

解决方案


推荐阅读