我已經定義像這樣的控制器:
app.controller("home", function ($scope, $http, $common) {
$http({
method: "GET",
url: '/posts/loadData'
}).then(function (response) {
//console.clear()
if (typeof response.data.posts != 'undefined') {
console.log(response.data.posts);
$scope.posts = $common.arrangePosts(response.data.posts);
}
});
})
並安排服務數據:使用
app.service('$common', function ($timeout, $sce, $httpParamSerializerJQLike) {
var that = this;
this.arrangePosts = function (rawPosts) {
var posts = [];
$.each(rawPosts, function (key, value) {
posts.push({
postId: value.postId,
postLink: '/post/' + that.cleanString(value.title) + '/' + value.postId,
title: value.title,
summary: $sce.trustAsHtml(value.summary)
});
});
return posts;
}
});
值在HTML這樣的:
<div class="widget fullwidth post-single">
<h4 class="widget-title">Latest</h4>
<div class="widget-content">
<ul>
<li ng-repeat="post in posts">
<h4 class="list-title"><a href="{{post.postLink}}">{{post.title}}</a></h4>
{{post.summary}}
</li>
</ul>
</div>
</div>
數據從服務器來在JSON格式:
Object { postId="4", title="asdf", summary="<p>asdf</p>"}
但是所有的html標籤都是打印在我的頁面上的,就像是一個文本一樣。
在許多SO帖子中,人們建議使用$sce.trustAsHtml
,但它不適合我。無論如何請解決我的問題。
任何幫助將不勝感激.. !!
你是否在頁面中注入了帶角的東西的html?也許顯示一個html的樣子的例子? – Yatrix
我已更新該問題,請檢查..! – Ritesh