Angular 2: functions/methods on click inside dynamic template (dynamic component)

江枫思渺然 提交于 2019-11-28 06:45:40

问题


Following the example below:

How can I use/create dynamic template to compile dynamic Component with Angular 2.0?

I developed my own template generator, which gets the HTML content directly from a variable. Here it is: http://plnkr.co/edit/2Sv1vp?p=preview

Now, my question is... if template content has to interact with the component, for example with a function to execute on click... how can I do that?

Here my app.component.ts

import { Component }          from '@angular/core';

@Component({
  selector: 'my-app',  
  template: `
    <div>
      <h2>An app with DYNAMIC content</h2>
      <hr />
      <dynamic-detail [tmpl]="tmpl1" [entity]="entity"></dynamic-detail>
      <dynamic-detail [tmpl]="tmpl2" [entity]="entity"></dynamic-detail>
    </div>`,
   })
   export class AppComponent { 
     private tmpl: string;
     private entity: any;

     constructor() {
       this.entity = { 
         code: "ABC123",
         description: "A description of this Entity",
         nome: "Bea"
       };

       this.tmpl1 = '<h2>Sono {{entity.nome}}, il primo template</h2>';
       this.tmpl2 = '<a (click)="printSomething()">Sono il secondo template</a>';
      }

    printSomething() {
      console.log("Hello World");
    }
}

When I try click on "Sono il secondo template", it should execute printSomething() function, but instead I obtain this error:

 Error in ./CustomDynamicComponent class CustomDynamicComponent - inline template:0:0 caused by: self.context.printSomething is not a function

回答1:


The problem is as Angular says; printSomething does not exist in your dynamically created component. If we declare a function within the dynamically created component, we are able to call it:

app.component.ts

this.tmpl2 = '<a (click)="linkClicked()">Sono il secondo template</a>';

type.builder.ts

  protected createNewComponent(tmpl: string) {
    @Component({
      selector: 'dynamic-component',
      template: tmpl,
    })
    class CustomDynamicComponent implements IHaveDynamicData {
      @Input() public entity: any;

      linkClicked() {
        console.log('yay!');
      }

    };
    // a component for this particular template
    return CustomDynamicComponent;
  }

If you want to call a method in app.component.ts, you'll need to pass a reference to it in a new @Input() attribute of CustomDynamicComponent.



来源:https://stackoverflow.com/questions/41148129/angular-2-functions-methods-on-click-inside-dynamic-template-dynamic-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!