如何通过字符串解析来自服务器的HTML? 我从服务器上得到了这个:
<img src="http://gravatar.com/avatar/9a52267d32ad2aaa4a8c2c45b83396e5?d=mm&s=&r=G" class=" user-1-avatar avatar- photo" width="" height="" alt="Avatar Image" />
通过对象属性进入,如:
{admin: {avatar "<img src="http://gravatar.com/avatar/9a52267d32ad2aaa4a8c2c45b83396e5?d=mm&s=&r=G" class=" user-1-avatar avatar- photo" width="" height="" alt="Avatar Image" />"}}
我尝试过使用ng-bind-html但没有运气...... 我可以使用什么?
答案 0 :(得分:1)
AngularJS的更高版本:
function HomeCtrl($scope, $sce) {
$scope.object = {
admin: {
avatar : '<img src="http://gravatar.com/avatar/9a52267d32ad2aaa4a8c2c45b83396e5?d=mm&s=&r=G" class=" user-1-avatar avatar- photo" width="" height="" alt="Avatar Image" />'
},
fred: {
avatar : '<img src="http://gravatar.com/avatar/9a52267d32ad2aaa4a8c2c45b83396e5?d=mm&s=&r=G" class=" user-1-avatar avatar- photo" width="" height="" alt="Avatar Image" />'
}};
$scope.avatar = $sce.trustAsHtml($scope.object.admin.avatar);
}
HTML:
<div ng-bind-html="avatar"></div>
工作演示:http://jsbin.com/butuwu/1/edit
旧版本:
HTML:
<div ng-bind-html-unsafe="avatar"></div>
JS:
function HomeCtrl($scope) {
var object = {
admin: {
avatar : '<img src="http://gravatar.com/avatar/9a52267d32ad2aaa4a8c2c45b83396e5?d=mm&s=&r=G" class=" user-1-avatar avatar- photo" width="" height="" alt="Avatar Image" />'
}};
$scope.avatar = object.admin.avatar;
}