0

I have some autogenerated html code highlighting some texts in my angularJS app. The line generating the html code is this one:

var replaced = original.replace(regEx, '<span style="background-color: red;">' + replacing + '</span>');
line[key + 'HL'] = replaced;

But writing this on template

<div><p>{{line.codeHL ? line.codeHL : line.code}} {{line.price}}</p></div>

shows this:

<span style="background-color: red;">GD5AU211</span> 102€

How can I force the evaluation of html code inside a string?

Also let's just say I can't use document.getElementById() for reasons.

p4x
  • 384
  • 1
  • 5
  • 16

3 Answers3

3

Refer to this link. This may be what you seek: AngularJS : Insert HTML into view

To enable html insertion into view you have to define html as trusted data using $sce.trustAsHtml()

Hope this would help.

Community
  • 1
  • 1
Parantap Parashar
  • 1,930
  • 1
  • 14
  • 22
0

if you are using angular 4 then use innerHTML

<div><p>

   <div [innerHTML]="line.codeHL ? line.codeHL : line.code">
    </div> {{line.price}}

</p></div>

if you are using angular 1 then use ng-bing-html

<div><p>
       <div ng-bind-html="line.codeHL ? line.codeHL : line.code | cust">
        </div> {{line.price}}
</p></div>

now create a cust filter like this

.filter('cust',function($sce){
  return function(html){
    return $sce.trustAsHtml(html)
  }
})
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
0

Add <xmp> tag before your html that has to be displayed as text.

var replaced = original.replace(regEx, '<span style="background-color: red;"><xmp>' + replacing + '</xmp></span>');
abhishek khandait
  • 1,990
  • 2
  • 15
  • 18