0

I am new to webdevelopment. So, Here, I have textAngular. In this I a, showing html document. Now, I want to show the text of the document only but that should be with the formatting as it is in the .html file. So,Right now my code is like -

here data is the html file content, you can say it's a string which has the html file content.Now,

$scope.htmlOriginalDoc = parser.parseFromString(data, 'text/html');
$rootScope.data.htmlDocument = $scope.htmlOriginalDoc.body.innerText;

So, Here when I get this that time I am getting all the data of that file, but when I see it it is like just a text document it is not having any new lines,I mean its not preserving the new line or spaces .So, How can I achieve this ?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
ganesh kaspate
  • 1
  • 9
  • 41
  • 88

1 Answers1

0

Basically you need to compile it from string, then bind it with ngSanitize. To preserve the white space wrap with <pre> tags.

var app = angular.module('myApp', ['ngSanitize']);
app.controller('myCtrl', function($scope, $sce) {
  $scope.html = "<div style=\"color:red;font-size:24px;\">    T   e  s t    </div>"; // string
  $scope.html = $sce.trustAsHtml($scope.html);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular-sanitize.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <div style="white-space: pre;"> <!-- instead of <pre> -->
    <div ng-bind-html="html"></div>
  </div>
</div>
Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34