在此答案之前,我不知道@Input() 和@Output(),将它们与*ngIf="" 结合使用我已经能够解决这个问题。
家长:
task-detail.component.ts
...
export class TaskDetailComponent implements OnInit {
extendedCustomer: boolean;
}
task-detail.component.html
...
<app-customerview [extendedCustomer]="false"></app-customerview>
...
孩子:
customerview.component.ts
import { Component, OnInit, Input } from '@angular/core';
...
export class CustomerViewComponent implements OnInit {
@Input() extendedCustomer: boolean;
}
customerview.component.html
<!-- Simple-->
...
<!-- Extended-->
<div *ngIf="extendedCustomer">
<p>detailed information here</p>
</div>
来源:https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-child
编辑:我不确定,但可能是您不需要在父级中定义布尔值(使 task-detail.component.ts 中的代码什么都不做)