I have components added dynamically into a page through a component factory. How can i bind the click event of each to a function (passing an id as argument for each)?. This is my code:
component/card-one/card-one.html:
<ion-card>
<ion-card-header>
{{ title }}
</ion-card-header>
<ion-card-content>
{{ data }}
</ion-card-content>
</ion-card>
component/card-one/card-one.ts:
import { Component, Input } from '@angular/core';
import { ProcessComponent } from '../process';
@Component({
selector: 'card-one',
templateUrl: 'card-one.html'
})
export class CardOneComponent implements ProcessComponent {
@Input() data: any;
@Input() title: any;
constructor() {}
}
And in the page/home/home.ts:
import { Component, ViewChild, ViewContainerRef, ComponentFactoryResolver } from '@angular/core';
import { NavController } from 'ionic-angular';
import { ProcessProvider } from '../../providers/process/process';
import { ProcessComponent } from '../../components/process';
import { CardOneComponent } from '../../components/card-one/card-one';
import { CardTwoComponent } from '../../components/card-two/card-two';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
@ViewChild('processContainer', { read: ViewContainerRef }) container;
constructor(public navCtrl: NavController, private processProvider: ProcessProvider, private resolver: ComponentFactoryResolver) { }
clickOnComponent(component){
//
}
ionViewDidLoad() {
let items = [
{
component: CardOneComponent,
desc: 'Mighty first step',
title: 'Hello'
},
{
component: CardTwoComponent,
desc: 'Always first looser',
title: 'Hello'
}
]
for (let card of cards) {
const factory = this.resolver.resolveComponentFactory(card.component);
let componentRef = this.container.createComponent(factory);
(<ProcessComponent>componentRef.instance).data = card.desc;
(<ProcessComponent>componentRef.instance).title = card.title;
}
}
}
Thanks!