Display HTML special characters in Angular 2 bindings?

后端 未结 7 1314
春和景丽
春和景丽 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}}
    

提交回复
热议问题