简体   繁体   English

我可以 select ng-content 在 ng-container(或其他)中吗?

[英]Can I select ng-content within ng-container (or otherwise)?

I have a component with an ng-content that selects a specific element but when I wrap the element in an ng-container , it does not render.我有一个带有ng-content选择特定元素的组件,但是当我将元素包装在ng-container时,它不会呈现。

I need the select of the ng-content to be specific so that I can exclude rendering of other elements so I can keep the code clean and as intended.我需要ng-content的 select 是特定的,这样我就可以排除其他元素的呈现,这样我就可以保持代码的清洁和预期。

Component:成分:

<ng-content select="input"></ng-content>

Content:内容:

<input id="1" />
<input id="2" />
<ng-container *ngIf="condition"> <!-- Condition has a thruthy value -->
    <input [id]="item + 2" *ngFor="let item of [1, 2, 3, 4]" />
</ng-container>

Output Output

<input id="1" />
<input id="2" />

Expected output预计 output

<input id="1" />
<input id="2" />
<input id="3" />
<input id="4" />
<input id="5" />
<input id="6" />

I tried both with ng-container and ng-template but neither seems to work.我尝试使用ng-containerng-template ,但似乎都不起作用。

Does anyone have an idea how to resolve this or if it even is possible?有谁知道如何解决这个问题,或者是否有可能解决这个问题?

Accomplish this with the ngProjectAs attribute使用ngProjectAs属性完成此操作

<ng-container ngProjectAs="input" *ngIf="true"> <!-- Condition has a thruthy value -->
  <input [id]="item + 2" *ngFor="let item of [1, 2, 3, 4]" />
</ng-container>
<ng-content select="input"></ng-content>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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