编辑:澄清任何只撇去标题的人,我的问题是关于Angular 2,而不是1.
我有一个组件模板,如下所示:
<div>{{ post.body }}</div>
Run Code Online (Sandbox Code Playgroud)
对象是这样的:
{
"title": "Some Title",
"body": "<p>The <em>post body</em>.</p>"
}
Run Code Online (Sandbox Code Playgroud)
而不是像下面那样渲染段落:
该帖子体
它显示:
"<p>The <em>post body</em>.</p>"
由于它是如此常见的任务,我寻找一个内置的管道,{{ post.body | safe }}但没有看到一个.
是否有一种简单的方法可以实现这一目标?是否有一种安全的方法来实现这一目标?
我看了很多,但我要么找不到答案,要么我不明白.一个具体的例子将赢得投票=)
这是我尝试过的:
// My magic HTML string function.
function htmlString (str) {
return "<h1>" + str + "</h1>";
}
function Ctrl ($scope, $compile) {
$scope.htmlString = htmlString;
}
Ctrl.$inject = ["$scope", "$compile"];
Run Code Online (Sandbox Code Playgroud)
那没用.
我也试过它作为一个指令:
// My magic HTML string function.
function htmlString (str) {
return "<h1>" + str + "</h1>";
}
angular.module("myApp.directives", [])
.directive("htmlString", function () {
return {
restrict: "E",
scope: { content: "@" },
template: "{{ htmlStr(content) }}"
}
});
... and in …Run Code Online (Sandbox Code Playgroud) 我有包含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)