Issue
I'm having a problem with printing out JSON data in HTML
var a = "We are the world.\n \tWelcome to our place.";
let's assume that I have this json data from the backend and I wanna print out something like this.
<div>
We are the world.
Welcome to our place.
</div>
I've tried with ng-bind-html but it shows all string inline.
Thanks.
Solution
You cannot do it simply. Because \n and \t are not recognized encoding for Html. You have to replace it with the recognized Html encoding first. You can do this by the following way:
$scope.a = "We are the world.\n \tWelcome to our place."
$scope.a.replace(/(?:\r\n|\r|\n)/g, '<br />');
But now if you bind this text into Html it will omit the extra spaces before the word Welcome and leave only one space.
So, to solve this problem you have to bind this Html inside a pre tag. It will preserve your formatting.
<pre>{{a}}<pre>
I have added a demo here: https://plnkr.co/edit/2twWjmWPCKGxcrh7QEgf?p=preview
Answered By - Md Hasan Ibrahim
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.