{{}}
- to interpolate strings, and the result will always be added as String. Binding does not work at all in this case due to the <
and >
contained in the expression, {{}}
not interpreted as expected.
<div [innerHTML]="replaceLineBreak(config.CompanyAddress) | safeHtml"></div>
from
replaceLineBreak(s:string) { return s && s.replace('\n','<br />'); }
should do what you want. As already mentioned, @hgoebl replaceLineBreak
can be implemented as a pipe if you need it in several places.
Plunger example
Tip . It is not recommended to directly contact methods, because the method is called in every change detection cycle. A clean (default) channel is called only when the input value changes. Therefore, the pipe is more efficient.
Another way is to replace only once and bind to a value with replaced line breaks, rather than repeating replaceLineBreak
.
Tip . You probably want to replace all line breaks, not just the first ones. one. There are enough JS questions that explain how to do this, so I was not worried.
source share