angular2-component-outlet icon indicating copy to clipboard operation
angular2-component-outlet copied to clipboard

Setting @Input properties on dynamic component

Open MarkPerryBV opened this issue 9 years ago • 6 comments

How do I set @Input bindings on my generated component.

MarkPerryBV avatar Sep 14 '16 10:09 MarkPerryBV

This is a working example, where you can pass data via Input, note the "context." in front of the passed variables.

app.component.ts

import { Component } from '@angular/core';

@Component({
    selector: 'my-app',
    template: `
        <div *componentOutlet="template; context: self; selector:'component'"></div>
        <input type="text" [(ngModel)]="title">
    `
})
export class AppComponent {
    self = this;
    title: string = "My Dynamic Component";
    descr: string = "This is a description"

    template = `<dynamic-component
        [title]='context.title'
        [descr]='context.descr'></dynamic-component>`;
}

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { DynamicModule } from './dynamic.module';

import { ComponentOutlet, provideComponentOutletModule } from 'angular2-component-outlet';

@NgModule({
    imports: [BrowserModule, FormsModule],
    declarations: [AppComponent, ComponentOutlet],
    providers: [
        provideComponentOutletModule({
            imports: [DynamicModule]
        })
    ],
    bootstrap: [AppComponent]
})

export class AppModule { }

dynamic.component.ts

import { Component, Input } from '@angular/core';

@Component({
    selector: 'dynamic-component',
    template: `
        <div>
            <h1>{{title}}</h1>
            {{descr}}
        </div>
    `
})
export class DynamicComponent {
    @Input() title;
    @Input() descr;

    constructor() {  }
}

dynamic.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

import { DynamicComponent } from './dynamic.component';

@NgModule({
    imports: [BrowserModule, FormsModule, CommonModule],
    declarations: [DynamicComponent],
    providers: [],
    exports: [DynamicComponent]
})

export class DynamicModule { }

j-moeller avatar Sep 14 '16 11:09 j-moeller

After the dynamic component replaces the *componentOutlet will it still do ngOnChanges stuff when I change context.title?

Awesome work btw. Thanks for the quick response.

MarkPerryBV avatar Sep 14 '16 11:09 MarkPerryBV

At least, it's supposed to do it

j-moeller avatar Sep 14 '16 11:09 j-moeller

Can I close this?

lacolaco avatar Oct 11 '16 01:10 lacolaco

I can't run the example, I using the code in my app

app.component.ts

import { Component } from '@angular/core';

@Component({
    selector: 'my-app',
    template: `
        <div *componentOutlet="template; context: self; selector:'component'"></div>
        <input type="text" [(ngModel)]="context.title">
    `
})
export class AppComponent {
    self = this;
    context = {
        title: "My Dynamic Component",
        descr : "This is a description"
    }

    template = `<dynamic-component
        [title]='context.title'
        [descr]='context.descr'></dynamic-component>`;
}

basexcc avatar Oct 20 '16 04:10 basexcc

Unfortunately, there have been breaking changes in #24

Removing the "context."-references should do the trick :)

j-moeller avatar Oct 20 '16 11:10 j-moeller