Hi i am using Angular 5 for my web application. My use case is to return some dynamically created html contents from Java service and same to be used in a angular component. I am able to bypass angular security by DomSanitizer, but still the click event is not working.
Below is my code.
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import DOMPurify from 'dompurify';
declare var angular: any;
@Pipe({
name: 'safeHtml'
})
export class SafeHtmlPipe implements PipeTransform {
constructor(protected sanitizer: DomSanitizer) {}
public transform(value: any): any {
const sanitizedContent = DOMPurify.sanitize(value);
console.log(sanitizedContent);
return this.sanitizer.bypassSecurityTrustHtml(sanitizedContent);
}
}
This is thee angular safeHtml pipe used to by pass non trusted html.
Component.ts - for service call
import { Component, OnInit } from '@angular/core';
import {ViewReviewService} from '../../services/view-review-service';
import {Response} from '@angular/http';
@Component({
selector: 'app-sample-hit',
templateUrl: './sample-hit.component.html',
styleUrls: ['./sample-hit.component.css']
})
export class SampleHitComponent implements OnInit {
sampleData: any;
constructor(private viewReviewService: ViewReviewService) { }
ngOnInit() {
this.hitSample();
}
hitSample() {
this.viewReviewService.hitSample().subscribe((res: Response) => {
console.log(res['_body']);
this.sampleData = res['_body'];
});
}
callSampleFunction(value: string) {
console.log('hit hit ' + value);
}
}
This.sampleData = '<a> This is my a tag</a>
<a (click)="callSampleFunction(value1)"> This tag will call function</a>
<a (click)="callSampleFunction(value2)"> This tag will call same function with
different value</a>
<p> other html </a>'
component.html
<div [innerHTML]="sampleData | safeHtml" >
</div>
What else i need to do. Please help ?