0

I am trying iterate over an array and create a DOM which is working fine but pipe is not working. I followed this example but this is also not working here.

Here is the json

[{
  "question":"What is this in QUestiom?",
  "ans":"<strong> this </strong> is only question"

},{
  "question":"What is the output of following code ?",
  "ans":"vgvfvfg"
},{
  "question":"What is the output of following code ?",
  "ans":"fcfcff"
}]

HTML

  <div *ngFor="let ques of questionseries;let i =index" class="test">
  <div>
    <span [innerHTML]="ques.question | safeHTML"> </span>
  </div>
  <div>{{ques.ans}}</div>
</div>

Custom Pipe

import { Pipe, PipeTransform } from '@angular/core';
import {DomSanitizer} from "@angular/platform-browser";

@Pipe({
  name: 'safeHTML',
  pure:false
})
export class SafeHTMLPipe implements PipeTransform {
 constructor(private sanitizer:DomSanitizer){}
  transform(value: any, args?: any): any {
   console.log(value , args);
    return this.sanitizer.bypassSecurityTrustHtml(value);
  }
 }

Here is the working DEMO.My expectation is this will be inside strong but it is only correctly rendering.

1 Answer 1

3

That line needs to bind to innerHTML as well :

  <div>{{ques.ans}}</div>

To be as follows:

  <div [innerHTML]="ques.ans | safeHtml"></div>
Sign up to request clarification or add additional context in comments.

1 Comment

oops you are right. I missed the very basic. I will accept it as answer

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.