• [Angular 2] Set Properties on Dynamically Created Angular 2 Components


    When you generate Angular 2 components, you’re still able to access the component instance to set properties and invoke methods from the component class.

    import {Component, ViewChild, ViewContainerRef, ComponentFactoryResolver, Input} 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;
    
        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);
            const comRef = this.container.createComponent(WidgetFactory); // return a componentRef
            comRef.instance.message = "I am last"; // using componentRef's instance prop to access the component prop
            comRef.instance.renderer.setElementStyle(
                comRef.instance.input.nativeElement,
                'color',
                'red'
            )
        }
    
    }

    widget-three.ts:

    import {Component, OnInit, ViewChild, Renderer, ElementRef, Input} from '@angular/core';
    
    @Component({
        moduleId: module.id,
        selector: 'widget-three',
        template: `<input type="text" #inputRef [value]="message"/>`
    })
    export class WidgetThree {
    
        @ViewChild('inputRef') input;
        @Input() message = "Default Value";
    
        constructor(private renderer: Renderer) {
        }
    
        ngAfterViewInit(){
            this.renderer.invokeElementMethod(
                this.input.nativeElement,
                'focus',
                []
            );
        }
    }

    So each time, when we call 'createComponent' method, it returns a ComponentRef instance. See https://angular.io/docs/ts/latest/api/core/index/ViewContainerRef-class.html

    We can use the instance to access the generated component's props, such as 'Renderer' or Input (message). Here we change the message value.

     comRef.instance.message = "I am last";

    And change the style thought Renderer:

            comRef.instance.renderer.setElementStyle(
                comRef.instance.input.nativeElement,
                'color',
                'red'
            )
  • 相关阅读:
    SQL对Xml字段的操作
    五种常见的ASP.NET安全缺陷
    EntityFramework中常用的数据删除方式
    002_ASP.NET 换主题
    001_ASP.NET MVC 实用教程 论坛项目 北盟网校 原创视频教程
    LINQ to Entities 比较日期
    windows10多桌面创建 切换 和分屏
    winform的combox下拉框绑定数据源
    C# 怎么让winform程序中的输入文本框保留上次的输入
    dos 批量重命名 bat
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5900691.html
Copyright © 2020-2023  润新知