[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>
时间: 2024-10-29 10:45:40

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

[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. You can store these TemplateRefs in a Service and then access them from any @Directive or @Component in your app. We want to create a service and a componen

[Angular] Test component template

Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'stock-counter', changeDetection: ChangeDetectionStrategy.OnPush, template: ` <div class="stock-counter"> &l

[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-angular2 ng serve Change the port: ng serve --port 4201 --live-reload-port 49153 Create a component: ng g component contact-list-component The component w

[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 passing them into your Subject.next() call. This lesson shows you how to make a number input and pass the value so you can configure how much you want the

[Angular 2] Share Template Content In Another Template With Content Projection &lt;ng-content&gt;

Angular 1 provided a mechanism to place content from your template inside of another template called transclusion. This concept has been brought into Angular 2 and was renamed to content projection and given super powers. In this lesson learn how to

[Angular 9] Built-in template syntax $any

The $any() type cast function Sometimes a binding expression triggers a type error during AOT compilation and it is not possible or difficult to fully specify the type. To silence the error, you can use the $any() cast function to cast the expression

[Angular] @ViewChild and template #refs to get Element Ref

We can use @ViewChild with component: @ViewChild(AuthMessageComponent) message: AuthMessageComponent; //.... ngAfterContentInit() { if (this.message) { this.message.days = 30; } } By doing this, we actually can access component's prop and events. If

[Angular Tutorial] 1-Static Template

为了说明Angular如何扩展了标准的html,您将会创建了一个纯粹的静态html页面,并且看到我们如何将这些html代码转换成Angular能动态展示的模板. 在这一步您将会在一个html页面中添加两部手机的基本信息. ·这个页面现在包含一个有两部手机的基本信息名单. 最重要的不同被列在了下面,您可以点击这里在GitHub上查看全部的不同. app/index.html: <ul> <li> <span>Nexus S</span> <p> F

ES6, Angular,React和ABAP中的String Template(字符串模板)

String Template(字符串模板)在很多编程语言和框架中都支持,是一个很有用的特性.本文将Jerry工作中使用到的String Template的特性做一个总结. ES6 阮一峰老师有一个专门的网站介绍ES6入门,还出了一本书: <ECMAScript6标准入门>. http://es6.ruanyifeng.com/ 我们来看看ES6里的String Template. 首先看下面这段代码. <html> <div id="JerryTest"