Javascript 添加带有ng repeat的html,其中html还包含ng repeat
我有这样的代码,它使用ng repeat将html代码绑定到divJavascript 添加带有ng repeat的html,其中html还包含ng repeat,javascript,html,angularjs,Javascript,Html,Angularjs,我有这样的代码,它使用ng repeat将html代码绑定到div <div ng-repeat="item in items" ng-bind-html="item.html"> 有什么建议可以让它工作吗?您必须首先使用$compile服务编译html。你可以看看这些文件 $compile您必须首先使用$compile服务编译html。你可以看看这些文件 $compileng bind html不会为您编译内容 您可以改为使用此指令,它基于ng bind html的源代码
<div ng-repeat="item in items" ng-bind-html="item.html">
有什么建议可以让它工作吗?您必须首先使用
$compile
服务编译html。你可以看看这些文件
$compile您必须首先使用
$compile
服务编译html。你可以看看这些文件
$compile
ng bind html
不会为您编译内容
您可以改为使用此指令,它基于ng bind html
的源代码,但经过修改后也可以编译:
app.directive('compileHtml', ['$sce', '$parse', '$compile',
function($sce, $parse, $compile) {
return {
restrict: 'A',
compile: function ngBindHtmlCompile(tElement, tAttrs) {
var ngBindHtmlGetter = $parse(tAttrs.compileHtml);
var ngBindHtmlWatch = $parse(tAttrs.compileHtml, function getStringValue(value) {
return (value || '').toString();
});
$compile.$$addBindingClass(tElement);
return function ngBindHtmlLink(scope, element, attr) {
$compile.$$addBindingInfo(element, attr.compileHtml);
scope.$watch(ngBindHtmlWatch, function ngBindHtmlWatchAction() {
element.html($sce.trustAsHtml(ngBindHtmlGetter(scope)) || '');
$compile(element.contents())(scope);
});
};
}
};
}
]);
用法:
<div ng-repeat="item in items" compile-html="item.html">
ng bind html
不会为您编译内容
您可以改为使用此指令,它基于ng bind html
的源代码,但经过修改后也可以编译:
app.directive('compileHtml', ['$sce', '$parse', '$compile',
function($sce, $parse, $compile) {
return {
restrict: 'A',
compile: function ngBindHtmlCompile(tElement, tAttrs) {
var ngBindHtmlGetter = $parse(tAttrs.compileHtml);
var ngBindHtmlWatch = $parse(tAttrs.compileHtml, function getStringValue(value) {
return (value || '').toString();
});
$compile.$$addBindingClass(tElement);
return function ngBindHtmlLink(scope, element, attr) {
$compile.$$addBindingInfo(element, attr.compileHtml);
scope.$watch(ngBindHtmlWatch, function ngBindHtmlWatchAction() {
element.html($sce.trustAsHtml(ngBindHtmlGetter(scope)) || '');
$compile(element.contents())(scope);
});
};
}
};
}
]);
用法:
<div ng-repeat="item in items" compile-html="item.html">
它应该像您编写的那样工作。你确定你的angular应用程序引导正确吗?你有任何控制台错误吗?它应该像你写的那样工作。你确定你的angular应用程序引导正确吗?你有任何控制台错误吗?