Angular 2 templates have a special let syntax that allows you to define and pass a context when they’re being generated.

import {Component, ViewChild, ViewContainerRef, ComponentFactoryResolver, ViewChild} from '@angular/core';
import {SimpleService} from "../../serivces/simple.service";
import {WidgetThree} from "../widgets/widget-three.component"; @Component({
moduleId: module.id,
selector: 'home',
templateUrl: 'home.component.html'
})
export class HomeComponent { @ViewChild('container', {
read: ViewContainerRef
}) container;
@ViewChild('template') template; constructor(private resolver: ComponentFactoryResolver, private simpleService: SimpleService) {
} ngAfterContentInit(){
const WidgetFactory = this.resolver.resolveComponentFactory(WidgetThree);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
} onClick(){
this.container.createEmbeddedView(
this.template,
{
desc: 'Good'
}
)
} }

We pass in an Object "desc: Good",  to the template, in tempalte, we use "let-" keyword to define the variable which we can reference to, and the object will be the 'context' for the template.

<template #template let-desc="desc">
<h3>tempalte {{desc}}</h3>
<section>tempalte {{desc}}</section>
<footer>template {{desc}}</footer>
</template>

[Angular 2] Create template with Params的更多相关文章

  1. R12: How to add Microsoft Excel as Type to the Create Template List of Values in BI Publisher (Doc ID 1343225.1)

    Modified: 27-Oct-2013 Type: HOWTO In this Document Goal Solution References APPLIES TO: BI Publisher ...

  2. [Angular Directive] Create a Template Storage Service in Angular 2

    You need to define a <template> to be able to use it elsewhere in your app as a TemplateRef. Y ...

  3. [Angular 2] Create Shareable Angular 2 Components

    Components that you use across multiple applications need to follow a module pattern that keeps them ...

  4. [Angular 2] Create Angular 2 Porject with Angular CLI

    Install: npm i -g angular-cli Create a project: ng new hello-angular2 Run the project: cd hello-angu ...

  5. [Angular 2] Create a simple search Pipe

    This lesson shows you how to create a component and pass its properties as it updates into a Pipe to ...

  6. [Angular 2] Passing Template Input Values to Reducers

    Angular 2 allows you to pass values from inputs simply by referencing them in the template and passi ...

  7. [Angular 2] Share Template Content In Another Template With Content Projection <ng-content>

    Angular 1 provided a mechanism to place content from your template inside of another template called ...

  8. [Angular] Test component template

    Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...

  9. 对angular实现延迟加载template和controller

    1.在lib目录中添加 script.js 文件,并在index.html其他<script>之前引用之: <script src="lib/script.js" ...

随机推荐

  1. JS代码的简单重构与优化

    JS代码的简单重构与优化(适合新手) 原文  http://www.cnblogs.com/similar/p/5016424.html Demo . 1 //bad if (age > 20) ...

  2. 【树莓派2B倒腾日志】之安装系统及配置

    15号树莓派到手到现在,折腾了也有一小周,自己摸索着,装了系统,登上SSH,更新了源,连了VNC,换上wifi,亮了小灯.再到今天捣鼓了下数码管,回头想想,该写个日志记录一下这一周的所得,自己总结也方 ...

  3. C#单元测试

    简单来说,单元测试就是局部测试,即是对项目中的某个静态类测试.静态方法测试.类的实例化测试以及类的方法测试.当您有一个具体的项目时您可以通过运行查看结果的方式进行测试,但当您只有一个类而没有完整的项目 ...

  4. BITED程序员语言学习心得之:C#语言基础

    一.HelloWorld 我们先来看看最简单的C#代码——HelloWorld: using System; using System.Collections.Generic; using Syste ...

  5. shell 内网主机存活探测器

    最近在学习shell 编程,闲来无事,搞了一个小shell. 可以用来 对一个网段的存活主机进行 探测. #!/bin/bash # #// #blog:www.cnblogs.com/outline ...

  6. 组建你自己的Theme,组件你的Style

    Andorid-Style,组建你自己的Theme,组件你的Style 前言: 今天,尝试了一个新的Demo,也尝试深入学习,话不多说,看一下,这个Demo如何实现的自定义主题与组件Style是如何绑 ...

  7. MySQL 5.6 警告信息 command line interface can be insecure 修复

    在命令行输入密码,就会提示这些安全警告信息. Warning: Using a password on the command line interface can be insecure.   注: ...

  8. hadoop2.6.0 --- 64位源代码

    今天有朋友在群里找hadoop最新的2.6.0的源代码,其实这个源代码在hadoop的官方网站是有下载的(应该是32位的),还有一个src,不过给的是maven版本,需要自己在机器上编译一下(我的机器 ...

  9. linux 操作总结

    1.集群节点之间ssh无密码登陆 参考:http://www.cnblogs.com/jdksummer/articles/2521550.html 解决: 1)删除旧有记录:rm -r ~/.ssh ...

  10. hdu 1155 Bungee Jumping

    http://acm.hdu.edu.cn/showproblem.php?pid=1155 Bungee Jumping Time Limit: 2000/1000 MS (Java/Others) ...