Sir*_*ert 73 html string angularjs
我看了很多,但我要么找不到答案,要么我不明白.一个具体的例子将赢得投票=)
这是我尝试过的:
// 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 my HTML ...
<html-string content="foo"></html-string>
Run Code Online (Sandbox Code Playgroud)
救命?
注意
除了别的以外,我看过这些,但无法使它发挥作用.
gan*_*raj 89
看看这个链接中的示例:
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"];
Run Code Online (Sandbox Code Playgroud)
然后在你控制器范围内的html中,你可以
<div ng-bind-html="htmlString"></div>
Run Code Online (Sandbox Code Playgroud)