我已经找了很多,但我要么找不到答案,要么就是我不明白。一个具体的例子将赢得投票=)
这是我尝试过的:
// My magic HTML string function.
function htmlString (str) {
return "<h1>" + str + "</h1>";
}
function Ctrl ($scope, $compile) {
$scope.htmlString = htmlString;
}
Ctrl.$inject = ["$scope", "$compile"];
这不管用。
我也试着把它作为一个指令:
// 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 my HTML ...
<html-string content="foo"></html-string>
帮助?
备注
我看了看这些,还有其他的,但是我不能让它工作。
发布于 2013-02-08 06:55:34
请看此链接中的示例:
http://docs.angularjs.org/api/ngSanitize.$sanitize
基本上,angular有一个将html插入页面的指令。在您的示例中,可以使用ng-bind-html指令插入html,如下所示:
如果您已经完成了所有这些操作:
// My magic HTML string function.
function htmlString (str) {
return "<h1>" + str + "</h1>";
}
function Ctrl ($scope) {
var str = "HELLO!";
$scope.htmlString = htmlString(str);
}
Ctrl.$inject = ["$scope"];
然后,在该控制器范围内的html中,您可以
<div ng-bind-html="htmlString"></div>
发布于 2015-10-21 11:50:45
您也可以使用$sce.trustAsHtml('"<h1>" + str + "</h1>"')
,如果您想了解更多详细信息,请参阅$sce
https://stackoverflow.com/questions/14761724
复制相似问题