How to replace <br/> in a filed value and print in new line in widget script

Chaiatnya
Tera Contributor

Hi all,

 

there is a html field and the filed value we are showing on portal. When printing the new lines are replaced with 

<br/> and I want to replace with new line in widget as well, but it is not working. But in when i troubleshoot in background script it is working. Can someone help how to do it in wodget.
 
  var lines = description.split("<br/>");
  var msg = lines.join('\r\n');
1 ACCEPTED SOLUTION

Cheikh Ahmadou
Tera Guru

You’re replacing <br/> with actual newline characters. This works in background scripts because it’s a backend context.

However, in the widget (browser/HTML context), the actual newline characters are not rendered visually in HTML. HTML ignores \n and \r\n unless inside elements like <textarea> or when explicitly styled.

1. if you already have <br/> in the value, then just use ng-bind-html in the widget template:

<div ng-bind-html="c.data.description"></div>

2. If you use ng-bind-html, and it doesn’t render the HTML tags properly, inject $sce to trust the content. And add this function on client side.

$scope.trustHtml = function(html) {
    return $sce.trustAsHtml(html);
  };

 
and then HTML will be:

<div ng-bind-html="trustHtml(c.data.description)"></div>

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@Chaiatnya 

where are you writing that code?

share some more details

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Chaiatnya
Tera Contributor

HI @Ankur Bawiskar have configured these lines in Server script on widget

@Chaiatnya 

then try this

var msg = description.replace(/<br\s*\/?>/gi, '\n');

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar Hi, still it is not working, it is not coming in new line 

Chaiatnya_0-1747820365354.png