简体   繁体   中英

Optionally pass data to child element via directive

I have a popup dialog box that I wish to display one of two slightly different views, dependant on whether I have data present or not.

Is there a way in which I can optionally pass in said data within the directives?

ie. I would like to use something like

<my-comp *ngIf="ifPopup" [data]="myData" [isNew]="isNew"></my-comp>

where the [data] may not always be present, ie. It may either be undefined or have actual data present.

I just want to avoid having to basically duplicate my component.

Update after confusion around my question Basically I am adding a new record to my DB (imagine adding a new customer etc). My component will either be used to edit a record or create a new record. If I am editing a record, myData will be filled with this record. If I am creating a new record, myData will be undefined..

why don't you just use

<my-comp *ngIf="ifPopup && myData" [data]="myData"></my-comp>

in this way, If the myData is not present, your popup wont display

或者,您也可以使用<ng-container>并将组件包装在其中,然后使用ngIf来存储数据!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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