部品工場で作成された動的コンポーネントのプロパティを変更すると、HTMLには影響しません。

FireShock:

私は、HTMLを生成し、印刷する新しいウィンドウで開きたいです。私がすべきは、HTMLを生成するコンポーネントがあります。

import { Component, ViewEncapsulation } from "@angular/core";
import { RelocateResponseVm } from "src/contracts/warehouse/relocateResponseVm";

@Component({
    selector: 'transition-print',
    styleUrls: ['./transition.component.scss'],
    encapsulation: ViewEncapsulation.None,
    templateUrl: './transition.print.component.html'
})
export class TransitionPrintComponent {
    public data: RelocateResponseVm;

    constructor() { }
}

そのテンプレートは次のとおりです。

<ng-container>
    <div>
        <h1>Operation №{{data.Id}}</h1>
        <p>Date: <b>{{data.OperationDate}}</b></p>
        <p>Name: <b>{{data.Name}}</b></p>
        <p>Description: <b>{{data.Description}}</b></p>
    </div>
</ng-container>

私は、次のコードでコンポーネントのインスタンスを作成します。

        const componentFactory = this.resolver.resolveComponentFactory(TransitionPrintComponent);
        let componentRef = componentFactory.create(this.injector);
        componentRef.instance.data = relocateDoc;

        console.log(componentRef.location.nativeElement);

かかわらず、relocateDoc満たされているnativeElement空です。

<transition-print>
<!---->
<div>
<h1>Operation №</h1>
<p>Date: <b></b></p>
<p>Name: <b></b></p>
<p>Description: <b></b></p>
</div>
</transition-print>

何が問題ですか?最終的なHTMLを埋めるためにどのように?さらに私は、新しいウィンドウにそれを置きます:

        this.OpenWindow = window.open('', 'childwindow', 'width=800,height=400,left=150,top=200');
        this.OpenWindow.document.body.innerHTML = "";
        this.OpenWindow.document.body.appendChild(componentRef.location.nativeElement);
AlexanderFSP:

良い一日!

あなたはおそらく正しいことを行う、しかし:

  1. あなたは必要としないng-containerだけで、それを削除し、
  2. の入力プロパティとしてマークデータTransitionPrintComponent@Input() data;
  3. 注入するViewContainerRefコンポーネントで、あなたがのインスタンスを作成してみてくださいTransitionPrintComponentそして、このコードスニペットを試してみてください。
const componentFactory = this.resolver.resolveComponentFactory(TransitionPrintComponent);
const componentRef = this.vcr.create(componentFactory);
componentRef.instance.data = relocateDoc;

console.log(componentRef.location.nativeElement);

ここのリンクなので、あなたがより多くの細部を得ることができます。https://angular.io/guide/dynamic-component-loader#resolving-components

それが役に立てば幸い ;)

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=377854&siteId=1