首页 > 代码库 > [Angular 2] Create template with Params
[Angular 2] Create template with Params
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。