簡體   English   中英

使用angular 4+在組件內部注入任何html /組件

[英]Inject any html/ component inside a component using angular 4+

我有一個彈出組件,我想向其中注入任何自定義組件。 目前,我正在使用[innerHTML],但無法呈現任何自定義組件。 我閱讀了https://angular.io/guide/dynamic-component-loader ,但是無法使用它來實現。 所以請提出建議。

popup.component.ts

<p-dialog [(visible)]="display" [responsive]="true" [blockScroll]=true (onHide)="onHide($event)" [width]="width" [resizable]="false">
    <p-header>
        <i>{{ title }}</i>
    </p-header>
    <div [innerHtml]="content|sanitizehtml"></div>
    <p-footer>
        <button type="button" pButton icon="pi pi-close" (click)="buttonClick($event)" [label]="noLabel" class="ui-button-secondary"></button>
        <button type="button" pButton icon="pi pi-check" (click)="buttonClick($event)" [label]="yesLabel"></button>
    </p-footer>
  </p-dialog>

app.componet.ts

    openLookup(e){
        this.display = true;
        this.title = 'Product ID';

        this.content = `<p-checkbox [(ngModel)]="someFlag" binary="true" (onChange)="changedRow()"></p-checkbox>
<p-calendar  name="endDate" [(ngModel)]="endDate" [monthNavigator]="true" [yearNavigator]="true" yearRange="1990:3000" [disabledDays]="[0,6]" [showIcon]="true" styleClass="full-width" inputStyleClass="calender-width">

                                    </p-calendar>`;

      }

嘗試將彈出html內容維護為子組件

您還可以與select屬性一起使用

父組件:

<child-component>
    <div sample>Parent Content</div>
</child-component>

子組件:

...
<ng-content select="[sample]"></ng-content>
...

輸出:

...
<div>Parent Content</div>
...

您也可以嘗試使用ng-container

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM