我有包含HTML的JSON变量.
通过做:{{source.HTML}}Angular显示<,>而不是<和>.
如何让Angular呈现实际的HTML?
更新:
这是我的控制器:
app.controller('objectCtrl', ['$scope', '$http', '$routeParams',
function($scope, $http, $routeParams) {
var productId = ($routeParams.productId || "");
$http.get(templateSource+'/object?i='+productId)
.then(function(result) {
$scope.elsevierObject = {};
angular.extend($scope,result.data[0]);
});
}]);
Run Code Online (Sandbox Code Playgroud)
在我的HTML中,我可以使用:
<div>{{foo.bar.theHTML}}</div>
Run Code Online (Sandbox Code Playgroud)
flo*_*bon 21
您想显示 HTML(如<b>Hello</b>)还是呈现 HTML(如Hello)?
如果你想展示它,花括号就足够了.但是,如果您拥有html实体(如<stuff),您需要手动取消它,请参阅此SO问题.
如果要渲染它,则需要使用该ng-bind-html指令而不是curcly括号(其中,FYI是ng-bind指令的快捷方式).您需要告诉Angular注入该指令的内容是安全的,使用$sce.trustAsHtml.
请参阅以下两种情况的示例:
angular.module('test', []).controller('ctrl', function($scope, $sce) {
$scope.HTML = '<b>Hello</b>';
$scope.trust = $sce.trustAsHtml;
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="test" ng-controller="ctrl">
<div>Show: {{HTML}}</div>
<div>Render: <span ng-bind-html="trust(HTML)"></span></div>
</div>Run Code Online (Sandbox Code Playgroud)