我有一個實現,其中父母想通過使用子組件可用的@Input
參數將某些數據傳遞給子組件。但是,此數據傳輸是可選項,父項可能會或可能不會按照要求傳遞它。是否有可能在組件中有可選的輸入參數。我描述了以下情況:Angular 2:組件交互,可選輸入參數
<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
感謝@galvan,它成功了! –