How to parse html that is embedded in a string inside a javascript object?

Jsfiddle

<script> angular.module('module', []); angular.module('module').controller('hello', function($scope) { $scope.obj = { "text": "<u>HelloWorld</u>" }; }); </script> <body> <div ng-app='module' ng-controller='hello'> Current:{{obj.text}} <br> <br> Expected:<u>HelloWorld</u> </div> </body> 

I am trying to read an object stored in JSON and then print it on my web page.

I have provided a link to the code above.

I expect the output to be the string "HelloWorld", which is underlined.

ps:

  • I can not edit JSON
  • obj is the object that is being extracted, I could not use JSON , so I used a solid value.
+5
source share
3 answers

You can use ng-bind-html and ng-bind-html-unsafe for this purpose. you must enable ngSanitize from angular-sanitize .

 <p ng-bind-html="obj.text"></p> 

An example is shown here.

+3
source

You want to just use regex like this:

 $scope.obj.text = $scope.obj.text.replace(/(<([^>]+)>)/ig,""); 

Working fiddle here

+1
source

You need to use the angular-sanitize module :

 <script src="path/to/installed/angular-sanitize/angular-sanitize.js"></script> <script> angular.module("module", ["ngSanitize"]); angular.module('module').controller('hello', function($scope) { $scope.obj = { "text": "<u>HelloWorld</u>" }; }); </script> 

And your html:

 <div ng-app='module' ng-controller='hello'> Current: <p ng-bind-html="obj.text"></p> <br> <br> Expected:<u>HelloWorld</u> </div> 
+1
source

Source: https://habr.com/ru/post/1257429/


All Articles