Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Popovers placed inside a innerHTML attribute work?

I have this printValue variable with a HTML button code inside my angular component like so:

export class HomeComponent implements OnInit {

  printValue = '<button type="button" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Popover</button>';

  constructor(){}
  OnInit(){}

}

The question is, how do i make the popover work once the printValue variable has been placed inside the innerHTML as seen below:

<p [innerHTML]="printValue"></p>

Doing this

<body>
<button type="button" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Popover</button>
</body>

inside an html file directly will enable popovers to work. But how to make it to work inside a innerHTML attribute?

like image 648
Dale Moncayo Avatar asked Jul 17 '18 04:07

Dale Moncayo


People also ask

How do I create a popover in HTML?

To create a popover, add the data-toggle="popover" attribute to an element. Use the title attribute to specify the header text of the popover, and use the data-content attribute to specify the text that should be displayed inside the popover's body: <a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside ...

What is the innerHTML property?

Definition and Usage The innerHTML property sets or returns the HTML content (inner HTML) of an element.

How to close the Popover when the element is outside?

Closing Popovers. By default, the popover is closed when you click on the element again. However, you can use the data-trigger="focus" attribute which will close the popover when clicking outside the element:

How does [innerHTML] handle XSS?

The rendered HTML could contain malicious scripts that present a security issue. One method of addressing XSS is by restricting the kinds of HTML elements and attributes to a set of known “safe” elements and attributes. Behind the scenes, [innerHTML] uses Angular’s DomSanitizer which uses a list of approved HTML elements and attributes.


3 Answers

In your html

<div #container></div>

Then, in your ts file,

 ....
export class MainDataComponent {

    @ViewChild('container') container: ElementRef;

    loadData(data) {// call this with your data
        this.container.nativeElement.innerHTML = data;
    }
}
like image 124
Malindu Sandaruwan Avatar answered Oct 13 '22 15:10

Malindu Sandaruwan


Do some change like in below code:

export class HomeComponent implements OnInit {
  printValue : string = '<button type="button" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Popover</button>';
  constructor(){}
  OnInit(){}
}

If it doesn't work then try ViewChild & ElementRef in your component:

import { ViewChild, ElementRef , Component  } from '@angular/core';

@Component({
    .......
})
export class YourComponent {


    @ViewChild('htmlId') htmlId: ElementRef;

      printValue : string = '<button type="button" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Popover</button>';


    addHTML(data) {
        this.htmlId.nativeElement.innerHTML = printValue;
    }
}

In your html:

Hope it will help you.

like image 1
Shubham Verma Avatar answered Oct 13 '22 14:10

Shubham Verma


By default angular sanitises html code to avoid any cross site scripting attacks, but we can skip that by using DOMSanitizer which tells angular that html content provided is safe. Look for reference :Correct way Provide DomSanitizer to Component with Angular 2 RC6

import {BrowserModule, DomSanitizer} from '@angular/platform-browser'

@Component({
  selector: 'my-app',
  template: `
     <p [innerHtml]="paragraphHtml"></p>
  `,
})
export class App {
  constructor(private domsanitizer: DomSanitizer) {
    this.paragraphHtml = domsanitizer.bypassSecurityTrustHtml('<p>My other child paragraph</p><script>any script function you want to execute</script><div>Thank you</div>') ;
  }
}

if this solution is no help, then other answers are all good, you can try.

Hope it helps.

like image 1
Abhishek Kumar Avatar answered Oct 13 '22 16:10

Abhishek Kumar