0

I have a JSON file which contains multiple <br /> tags. The file is parsed using JSON.parse(json) into an object. Because I bind the data with AngularJS and ng-repeat I don't want that the strings have any HTML tags and replace it with a new line \n. How can I replace all tags? It seems to me that replace() only works with strings.

Thanks for your help!

JSON example

{
    "title": "Title",
    "description": "This<br />is<br />a<br />description."
}

JavaScript

var retrievedObject = JSON.parse(json);
$scope.data = retrievedObject;

HTML

<div ng-repeat="item in data">
    {{item.description}}
    {{item.description}}
</div>
ohh2ahh
  • 385
  • 1
  • 2
  • 14
  • 1
    possible duplicate of [angularjs to output plain text instead of html](http://stackoverflow.com/questions/17289448/angularjs-to-output-plain-text-instead-of-html) – ebo May 12 '14 at 17:48

3 Answers3

2

You could just replace it before you parse the string

var retrievedObject = JSON.parse(json.replace(/\<br \/\>/g, ''));

A better option would be to parse the strings as HTML and extract the text without the tags, not using a regex before you insert them in the DOM

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Thanks for your help. I forgot to mention that I want to replace the tags with a new line and edited my post while you answered my question. Sorry. – ohh2ahh May 12 '14 at 17:48
2

There's two ways to do what you're looking for. One with css where you replace the br's with \n and then in your css file give the element the white-space property of pre-wrap.

The other is angularish. Take a read of data-ng-bind-html. You'd be able to actualy have the br's outputted. https://docs.angularjs.org/api/ng/directive/ngBindHtml You would have to run it through a filter of $sce so that it's trusted but your code would be as simple as:

<div ng-repeat="item in data">
    <span data-ng-bind-html="item.description | trusted"></span>
    <span data-ng-bind-html="item.description | trusted"></span>
</div>

your filter for trusted would be this:

.filter("trusted", function($sce){
    return function(input){
        return $sce.trustAsHtml(input);
    }
});
Mathew Berg
  • 28,625
  • 11
  • 69
  • 90
1

ngBindHtml will let you keep keep the <br>s and render newlines in your HTML. It will automatically sanitize your input using ngSanitize to strip out any tags are aren't in its whitelist (you have to bring ngSanitize in as a dependency)...

var app = angular.module("app", ["ngSanitize"]);

The view is as simple as...

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

JsBin

You can also use $sce.trustAsHtml() to tell ngBindHtml to blindly trust the HTML without sanitizing, but only do that if you can completely trust its content (i.e., not for things like user submitted comments, etc).

Anthony Chu
  • 37,170
  • 10
  • 81
  • 71
  • Thank you, this is exactly what I needed! I've tried `ng-bind-html` but I didn't know about `ngSanitize` and bringing it in as a dependency. – ohh2ahh May 12 '14 at 20:09