Display HTML special characters in Angular 2 bindings?

后端 未结 7 1316
春和景丽
春和景丽 2020-12-14 15:46

I have a normal binding like this {{foo}} and it displays foo\'s value as text in the HTML. The text that comes from the server is \"R&D\"

相关标签:
7条回答
  • 2020-12-14 16:46

    For a little more fun and flexibility, you can create a pipe that parses HTML entities:

    @Pipe({name: "decodeHtmlString"})
    export class DecodeHtmlString implements PipeTransform {
        transform(value: string) {
            const tempElement = document.createElement("div")
            tempElement.innerHTML = value
            return tempElement.innerText
        }
    }
    
    {{foo | decodeHtmlString}}
    
    0 讨论(0)
提交回复
热议问题