zoukankan      html  css  js  c++  java
  • [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>
  • 相关阅读:
    时序数据库入门
    MySQL执行计划extra中的using index 和 using where using index 的区别
    ETL的详细解释定义
    六种 主流ETL 工具的比较(DataPipeline,Kettle,Talend,Informatica,Datax ,Oracle Goldengate)
    使用 IntraWeb (37)
    使用 IntraWeb (36)
    使用 IntraWeb (35)
    使用 IntraWeb (34)
    使用 IntraWeb (33)
    使用 IntraWeb (32)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5901625.html
Copyright © 2011-2022 走看看