There is a chance of duplication, but my scenario is slightly different.
I want to execute a click event for my dynamic component.
here is my structure:
<razor> <mvc-partial> <dynamic-html> // buttonPress(){console.log("Function called in dynamicHtml) // Output() to call function in razor.ts </dynamic-html> </mvc-partial> <razor>
File RenderingViewDynamic.ts
import { Component, Directive, NgModule, Input, Output, EventEmitter, ViewContainerRef, Compiler, ComponentFactory, ModuleWithComponentFactories, ComponentRef, ReflectiveInjector, OnInit, OnDestroy, ViewChild } from '@angular/core'; import { RouterModule } from '@angular/router'; import { CommonModule } from '@angular/common'; import { Http } from "@angular/http"; import 'rxjs/add/operator/map'; export function createComponentFactory(compiler: Compiler, metadata: Component): Promise<ComponentFactory<any> | undefined>{ console.log(compiler) console.log(metadata) class DynamicComponent { @Output() buttonType: EventEmitter<string> = new EventEmitter<string>() // button click operation buttonPress() { this.buttonType.emit(); } }; const decoratedCmp = Component(metadata)(DynamicComponent); @NgModule({ imports: [CommonModule, RouterModule], declarations: [decoratedCmp] }) class DynamicHtmlModule { } return compiler.compileModuleAndAllComponentsAsync(DynamicHtmlModule) .then((moduleWithComponentFactory: ModuleWithComponentFactories<any>) => { console.log(decoratedCmp) console.log(moduleWithComponentFactory.componentFactories.find(x => x.componentType === decoratedCmp)) return moduleWithComponentFactory.componentFactories.find(x => x.componentType === decoratedCmp); }); } @Component({ selector: 'mvc-partial', template: `<div #dynamicHtml></div>` })
razor.component.html
<mvc-partial [url]="'/View/Index'" (buttonType)="click()"></mvc-partial>
razor.component.ts
click(){ console.log("In razor") }
My question is: the button in my dynamic html wants to bind this event to work in razor.ts.
like <dynamic-html>-<mvc-partial>-<razor>
how could I achieve it?
update . Trying to contact the service
class DynamicComponent { constructor(private appService: AppService) { } buttonPress() { this.appService.onButtonClickAction.emit() } }; const decoratedCmp = Component(metadata)(DynamicComponent); @NgModule({ imports: [CommonModule, RouterModule], declarations: [decoratedCmp], providers: [AppService] })
An error pops up: Unable to resolve all parameters for DynamicComponent: (?).
The above error is resolved by adding @Inject(forwardRef(() => AppService)
constructor( @Inject(forwardRef(() => AppService)) private appService: AppService) { }