[英]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. [data]
可能并不总是存在的地方,即 It may either be undefined
or have actual data present. 它可能是undefined
或者有实际数据。
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. 如果我正在编辑一条记录,则myData将被该记录填充。 If I am creating a new record, myData will be undefined.. 如果我要创建新记录,则myData将是未定义的。
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 这样,如果myData
不存在,则不会显示您的弹出窗口
或者,您也可以使用<ng-container>
并将组件包装在其中,然后使用ngIf来存储数据!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.