首页 > 其他 > 详细

[Angular] Using ngTemplateOutlet to create dynamic template

时间:2017-03-06 21:01:15      阅读:437      评论:0      收藏:0      [点我收藏+]

I can use <tamplete> syntax and a entry component as a container to create a dynamic component. Notice it will create a empty div as a placeholder in the DOM. If we don‘t wanner this empty div, we can actually using ng-conainer together with ngTemplateOutlet (for template ref) and ngTemplateOutletContext (the context).

 

import { Component, TemplateRef, ComponentRef, ViewContainerRef, ViewChild, AfterContentInit, ComponentFactoryResolver } from @angular/core;

import { AuthFormComponent } from ./auth-form/auth-form.component;

import { User } from ./auth-form/auth-form.interface;

@Component({
  selector: app-root,
  template: `
    <div>
      <div #entry></div>
      <template #tmpl let-obj let-location="location">
          <details>
            <summary>{{obj.name}}</summary>
            <p> - Age: {{obj.age}}</p>
            <p> - Address :{{location}}</p>
          </details>
      </template>
      <hr />
      <ng-container 
      [ngTemplateOutlet]="tmpl"
      [ngTemplateOutletContext]="ctx"
      ></ng-container>
    </div>
  `
})
export class AppComponent implements AfterContentInit {

  @ViewChild(entry, { read: ViewContainerRef }) entry: ViewContainerRef;
  @ViewChild(tmpl) tmpl: TemplateRef<any>;

  ctx = {
    $implicit: {
      name: John,
      age: 34
    },
    location: USA
  }

  ngAfterContentInit() {
    this.entry.createEmbeddedView(this.tmpl, {
      $implicit: {
        name: Zhentian,
        age: 27
      },
      location: China
    })
  }

}

 

技术分享

 

And in the generated DOM we can see that there is no empty div created.

[Angular] Using ngTemplateOutlet to create dynamic template

原文:http://www.cnblogs.com/Answer1215/p/6511531.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!