2017-12-22 5 views
1

私は、角でdom操作を学ぶために凝視しており、templateRefとそのメソッドcreateEmbeddedViewについて気づいています。この方法についてもっと知りたいと思っています。angle4のTemplateRefのcreateEmbeddedViewメソッドの使い方は?

@Component({ 
    selector: 'app-root', 
    template: ` 
     <ng-template #template let-name='fromContext'><div>{{name}}</ng-template> 
     <ng-container #vc></ng-container> 
    ` 
}) 
export class AppComponent implements AfterViewChecked { 
    @ViewChild('template', { read: TemplateRef }) _template: TemplateRef<any>; 
    @ViewChild('vc', {read: ViewContainerRef}) vc: ViewContainerRef; 
    constructor() { } 

    ngAfterViewChecked() { 
     const view = this._template.createEmbeddedView({fromContext: 'John'}); 
     this.vc.insert(view); 
    } 
} 

それともできます。今、すべての私の質問は、あなたが、その後ViewContainerRef経由でDOMにそのビューを添付createEmbeddedView方法を使用して埋め込まれたビューを作成することができます

@Component({ 
    selector: 'app-root', 
    template: ` 
<ng-template #template> 

</ng-template> 
    ` 
}) 
export class AppComponent implements AfterViewChecked { 
     @ViewChild('template', { read: TemplateRef }) _template: TemplateRef<any>;  
    constructor() { } 

    ngAfterViewChecked() { 
    this._template.createEmbeddedView('this is a embedded view') 
    } 
} 

答えて

1

この方法のcreateEmbeddedViewを使用する方法であり、

ngAfterViewChecked() { 
    this.vc.createEmbeddedView(this._template, {fromContext: 'John'}); 
} 

コンテキストはプロパティを持つオブジェクトであり、あなたはthrouこれらのプロパティにアクセスすることができます直接ViewContainerRefを使用してビューを作成しますgh let-バインディング。

詳しくはExploring Angular DOM manipulation techniques using ViewContainerRefを参照してください。this answerもご覧ください。

+0

コンテキストタイプは何ですか、文字列ですか? vcとは何ですか?viewContainerRefですか? –

+0

@ LijinDurairaj、私は答えを –

+0

はい、私は理解したが、私の質問は、あなたのコードでも言及した "コンテキスト"タイプは何ですか。 は、値ieをとる文字列です。

this is a dynamic element

関連する問題