0

This is my code: https://angular-gcmti7.stackblitz.io

In the left side, I have grid patterns which help in generating HTML layout. These patterns have to be dragged on the empty panel.

I'm facing problem in triggering the click event on the menu generated on the elements. I have to append dynamic menu to the dynamically generated HTML blocks. These can be N in numbers depending upon the selection.

I have tried these, but not working: 1) Child component rendering for menu // selectors is appending statically. 2) Direct HTML rendering // HTML block appending statically.

Please suggest a way to get the click event working on these menu icons.

Prachi
  • 3,478
  • 17
  • 34

1 Answers1

1

Your logic seems a little different from the regular Angular development. It took almost a day to try a solution for you but I didn't come with the exact solution but hope the below findings of mine could help you.

  1. The ComponentFactoryResolver's resolveComponentFactory method accepts an Angular Component.

  2. An Article over dynamic HTML injection https://medium.com/@caroso1222/angular-pro-tip-how-to-dynamically-create-components-in-body-ba200cc289e6

  3. In your case, you are injecting HTML into your template, not a component. To inject HTML, save it in a variable and use the DomSanitizer to either sanitize it or bypass the security check

This might help you.

export class main_page{

data: SafeHtml;

  constructor(private sanitizer: DomSanitizer) {}      

  ngOnInit(){ 
    this.getDynamicREST().then((res)=> {
        this.data = this.sanitizer.sanitize(res);
        /* OR */
        this.data = this.sanitizer.bypassSecurityTrustHtml(res);
    })
  };
}

Then, in your template:

<div class="top">
  <div [innerHtml]="data"></div>
</div>
Rohit.007
  • 3,414
  • 2
  • 21
  • 33