【发布时间】:2017-02-04 15:18:51
【问题描述】:
我有一个实现,其中父组件希望通过使用子组件可用的@Input 参数将某些数据传递给子组件。但是,此数据传输是可选的,父级可能会也可能不会根据要求传递它。组件中是否可以有可选的输入参数。我在下面描述了一个场景:
<parent>
<child [showName]="true"></child> //passing parameter
<child></child> //not willing to passing any parameter
</parent>
//child component definition
@Component {
selector:'app-child',
template:`<h1>Hi Children!</h1>
<span *ngIf="showName">Alex!</span>`
}
export class child {
@Input showName: boolean;
constructor() { }
}
【问题讨论】:
-
是的,您可以有可选输入,如果输入已初始化,请检查 ngAfterViewInit 生命周期事件
-
感谢@galvan,它成功了!
标签: angular angular2-components