我从数据库中以 json 对象的形式获取我的文章。在这个 json 中,我有我的文章数据,如标题和正文。我可以通过 {{title}} 在 html 页面中显示它,但我的问题是关于正文的。主体包含 HTML 代码,但 Angular 将其显示为 2 个引号之间的字符串。怎么解决呢?
Angular Controller 代码:
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;
});
});
HTML View :
<h2>{{title}}</h2>
<p>{{body}}</p>
结果:
<p><iframe src="https://www.youtube.com/embed/9aOuF0-1KKY" width="560" height="315" frameborder="0" allowfullscreen=""></iframe></p>
结果中的html代码不能作为html代码使用 结果图像: enter image description here enter image description here
最佳答案
<p ng-bind-html="body"></p>
关于javascript - 如何在 Angularjs 中将 json 响应显示为代码而不是字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40631199/