javascriptangulartypescriptangular-templateangular-dom-sanitizer

Angular 9 template interpolation removing backslashes


I need to render a string on the page with double backslash "\\" but Angular removes one from the temaplte, treating it like a regular expression.

An example of the issue is here: https://codepen.io/bental/pen/xxZdYZv

I have also tried escaping with and without DomSanitizer but can't seem to make it work.

I don't want to manipulate the data as it's coming from the backend and can't account for every possibility


Solution

  • If you don't need IE/Opera support, you can use String.raw (note that you need to use backticks instead of double quotes for it to work as intended)

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw

    this.sanitizer.bypassSecurityTrustHtml(String.raw`Double backslashes are being removed -> \\`);