有可能重复,但我的情况有点不同。
我想为动态组件执行click事件。
这是我的结构:
<razor>
<mvc-partial>
<dynamic-html> // buttonPress(){console.log("Function called in dynamicHtml)
// Output() to call function in razor.ts
</dynamic-html>
</mvc-partial>
<razor>
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>`
})
//@Directive({ selector: 'mvc-partial' })
export class RenderingViewDynamic implements OnInit {
@ViewChild('dynamicHtml', { read: ViewContainerRef }) target: ViewContainerRef;
html: string = '<p></p>';
@Input() url: string;
cmpRef: ComponentRef<any>;
constructor(private vcRef: ViewContainerRef, private compiler: Compiler, private http: Http) { }
ngOnInit() {
this.http.get(this.url)
.map(res => res.text())
.subscribe(
(html) => {
this.html = html;
if (!html) return;
if (this.cmpRef) {
this.cmpRef.destroy();
}
const compMetadata = new Component({
selector: 'dynamic-html',
template: this.html,
});
createComponentFactory(this.compiler,compMetadata)
.then(factory => {
//const injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector);
this.cmpRef = this.target.createComponent(factory!);
});
},
err => console.log(err),
() => console.log('MvcPartial complete')
);
}
ngOnDestroy() {
if (this.cmpRef) {
this.cmpRef.destroy();
}
}
}
razor.component.html
<mvc-partial [url]="'/View/Index'" (buttonType)="click()"></mvc-partial>
razor.component.ts
click(){
console.log("In razor")
}
我的问题是,按钮在我的dynamic-html中想要将它绑定到razor.ts中的函数。
就像<dynamic-html>-<mvc-partial>-<razor>
我怎么能实现它?
更新:尝试与服务进行通信
class DynamicComponent {
constructor(private appService: AppService) { }
buttonPress() {
this.appService.onButtonClickAction.emit()
}
};
const decoratedCmp = Component(metadata)(DynamicComponent);
@NgModule({ imports: [CommonModule, RouterModule], declarations: [decoratedCmp], providers: [AppService] })
错误弹出:无法解析DynamicComponent的所有参数:(?)。
通过添加@Inject(forwardRef(() => AppService)
constructor( @Inject(forwardRef(() => AppService)) private appService: AppService) { }
答案 0 :(得分:0)
您可以使用EventEmitter:
将您孩子的点击事件冒泡到您的父母<%= react_component("HelloWorldApp", props: @hello_world_props , prerender: false) %>
编辑:
如果您想根据评论来访问您的子组件:clickEmitter = new EventEmitter();
clickEmitter.emit();
<your-child-component (clickEmitter)="functionInParent()"></your-child-component>
您可以使用#标签(#)和主题标签的名称或组件名称来引用组件
<dynamic-html>-<mvc-partial>-<razor>
等等。