5

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 a JSON and then print it on my web page.

I have provided a link to the code above.

I am expecting the output to be a string "HelloWorld" which is underline.

ps:

  • I cannot edit the JSON
  • obj is the object that is being fetched, I could not share the JSON so i have used a hardcoded value.
2
  • There isn't any JSON shown in your code. You have an object. Commented Sep 29, 2016 at 8:18
  • Correct, I used a dummy object, but that object is being fetched from a JSON,since i couldn't share the entire JSON file here, i hardcoded the object in the script. Thanks for the suggestion Commented Sep 29, 2016 at 9:28

3 Answers 3

3

You can use ng-bind-html and ng-bind-html-unsafe for that kind of purpose. you have to include ngSanitize from angular-sanitize.

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

The example is shown here

Sign up to request clarification or add additional context in comments.

Comments

1

You want to just use a regular expression like so:

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

Working fiddle here

1 Comment

I want the <u> tag to get applied to the String rather than removing it.
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.