0

I am trying to set up a system which loads the html page from a stored html file and renders it with angular. How to bind property and event in this file, I'm trying to inject it with innerHTML.

ParentComponent.ts

genericAttribut ="Hello"

getHtmlContent(){
    id =1;
    this.service.getHtmlContent(id).subscribe((data)=>
    myTemplate =data;
});
}  
genericFunction(){
console.log("fire");
}

ParentComponent.html

<div [innerHTML]="myTemplate | safeHtml"></div>

Template-1.html

<button (click)="genericFunction()">{{genericAttribut}}</button>

When i try this, angular don't do the binding.

3

1 Answer 1

1

Template-1.html

<button id="buttonId">Button Name</button>

ParentComponent.ts

getHtmlContent(): void {
    id = 1;
    this.service.getHtmlContent(id).subscribe((data) => {
      myTemplate = data;

      // Call this after updating innerHtml content
      setTimeout(() => {
        let buttonElement = document.querySelector('#buttonId');
        if(buttonElement) {
          buttonElement.addEventListener('onclick', (event) => {
            this.genericFunction();
          })
        }
      }, 1000)
    });
  }
  genericFunction() {
    console.log("fire");
  }

Related Angular 2 innerHTML (click) binding

Sign up to request clarification or add additional context in comments.

1 Comment

this.genericFunction is not a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.