【发布时间】:2018-02-02 01:08:51
【问题描述】:
有可能重复,但我的情况有点不同。
我想为我的动态组件执行点击事件。
这是我的结构:
<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")
}
我的问题是,按钮在我的动态 html 中,想要将它的事件绑定到 razor.ts 中。
like <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) { }
【问题讨论】:
-
您不能将
@Output()或@Input()与动态添加的组件一起使用。请参阅我在stackoverflow.com/questions/36325212/… 中的答案中第一个代码块中的注释掉的代码,以了解如何与动态添加的组件进行通信。也可以使用共享服务。 -
@GünterZöchbauer stackoverflow.com/questions/40725620/… 对我有什么帮助?
-
为动态添加的 HTML 添加点击处理程序
-
但我的问题是关于
DynamicComponent有按钮我在 DynamicComponent 中处理它。buttonPress() { this.buttonType.emit(); }检查我的问题。buttonType()是razor.ts中需要调用的那个。作为您的第一条评论,它可能也称为共享服务,但重复不合理。 -
我明白了。我认为这是在标记为重复之后添加的。对不起,错了。
标签: angular typescript