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

@Chaiatnya 

sorry but with limited context I can't help.

you just shared 2 lines and didn't share what you are doing in that widget, what the complete script looks like, what's your business requirement

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

Thanks @Ankur Bawiskar  for your support as always 🙂

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>

Thanks @Cheikh Ahmadou  , it worked

Cheikh Ahmadou
Tera Guru

You're welcome!
Glad i've been helpful.