How to render an html that came from a json's answer?

1

JSON response:

post: {
   body: "<div>O meu html vem dessa forma</div>"
}

I would like to know how I can render this% my post.body in my view. Is there any directive that will do this for me ???

    
asked by anonymous 05.01.2017 / 17:42

3 answers

1

With this kind of response in HTML format you would need to load the angle in ngSanitize and use tag for loading ng-bind-html ( angular.module('app', ['ngSanitize']); ).

Minimum example:

var app = angular.module('app', ['ngSanitize']);
app.controller('ctrl', ['$scope', function($scope) 
{
     $scope.result = '';        
     $scope.submit = function()
     {
         var post = {
            body: "<div>O meu <i>html</i> <b>vem</b> dessa forma</div>"
         }
         $scope.result = post.body;
       
     }
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-sanitize.js"></script><divng-app="app" ng-controller="ctrl">
  <p ng-bind-html="result"></p>
  <a href="#" ng-click="submit();">Clique para carregar</a>
</div>

05.01.2017 / 19:38
1

See this example in the fiddle, you will need a decode in the body variable and an append in the element that will receive the html: link

    
05.01.2017 / 17:47
1

If you have not yet done so, you can use ng-bind-html

    
05.01.2017 / 19:12