如何在Angularjs中将json响应显示为代码而不是字符串?

时间:2016-11-16 11:39:00

标签: javascript html angularjs json string

我从数据库中获取我的文章作为json对象。在这个json我有我的文章数据,如标题和正文。我可以通过{{title}}在html页面中显示它,但我的问题是关于身体。 body包含HTML代码,但angular将其显示为2引号之间的字符串。我该如何解决?

角度控制器代码:

.captcha img
  {
     position:absolute; 
  }

Html视图:

myApp.controller("articlePageController" , function($scope , $http        ,$location , $routeParams){
$http.get("/getArticle")
    .success(function(data) {
        $scope.body = data["article"]["body"];
        $scope.title = data["article"]["title"];
    })
    .error(function(data){
        console.log("no");
        $scope.currentArticle = data;
    });
});

结果:

<h2>{{title}}</h2>
<p>{{body}}</p>

结果中的html代码不能用作html代码 结果图片: enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

使用ngBindHtml

<p ng-bind-html="body"></p>

答案 1 :(得分:0)

您需要使用ng-bind-html来显示html内容。 https://docs.angularjs.org/api/ng/directive/ngBindHtml