2017-08-24 132 views
0

有可能重複的機會,但我的情況有點不同。動態組件點擊事件綁定 - 角2

我想爲我的動態組件執行點擊事件。

這裏是我的結構:

 <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所有參數:(?)。

clickEmitter = new EventEmitter(); 

clickEmitter.emit(); 

<your-child-component (clickEmitter)="functionInParent()"></your-child-component> 

編輯:

enter image description here

上述錯誤加入@Inject(forwardRef(() => AppService)

constructor(@Inject(forwardRef(() => AppService)) private appService: AppService) { } 
+0

不能使用'@Output()'或'@input ()'動態添加組件。請參閱https://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468中的答案中的第一個代碼塊中的已註釋代碼如何與動態添加的組件進行通信。共享服務也可以使用。 –

+0

@GünterZöchbauerhttps://stackoverflow.com/questions/40725620/angular-2-dynamic-component-click-event它將如何幫助我? – k11k2

+0

要添加一個點擊處理程序來動態添加HTML –

回答

0

你可以泡了,從你的孩子你的父母click事件通過EventEmitter解決如果您想按照您的評論訪問您的孩子組件:<dynamic-html>-<mvc-partial>-<razor>您可以參考使用#標籤的主題標籤(#)和名字的部件或組件名稱

@ViewChild(MvcPartialComponent) mvcPartialComponent: MvcPartialComponent; 

<mvc-partial #mvc-partial></mvc-partial> 
@ViewChild('mvc-partial') mvcPartialComponent: MvcPartialComponent; 

等等等等

+0

無法爲上述情況做到這一點。原因流程是' - - '按鈕在'dynamic-html'中,函數應該在'razor'中調用。 – k11k2

+0

然後你可以在組件上設置一個引用並直接調用這個函數。看到我更新的答案 – Carsten

+0

是啊。但事情是我的按鈕是'dynamic-html'我在相應的'DynamicComponent'中處理它,所以'MvcPartialComponent'的引用無法工作。 Parent'razor'-Child'mvc-partial'-subChild(DynamicComponent)'dynamic-html' – k11k2