簡體   English   中英

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

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

我有一個帶有ng-content選擇特定元素的組件,但是當我將元素包裝在ng-container時,它不會呈現。

我需要ng-content的 select 是特定的,這樣我就可以排除其他元素的呈現,這樣我就可以保持代碼的清潔和預期。

成分:

<ng-content select="input"></ng-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

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

預計 output

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

我嘗試使用ng-containerng-template ,但似乎都不起作用。

有誰知道如何解決這個問題,或者是否有可能解決這個問題?

使用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