Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...