Heim > Fragen und Antworten > Hauptteil
Problem: Mit ng-bind-html wurde der richtige HTML-Code auf der Seite generiert, aber die Anweisung im Tag wird nicht wirksam!
js-Code:
HTML-Code:
怪我咯2017-06-16 09:21:14
当然无法生效,ng-bind-html
等同于 innerHTML
。
可以自定义一个类似 ng-bind-html-compile
的指令:
.directive('bindHtmlCompile', ['$compile', function ($compile) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.$watch(function () {
return scope.$eval(attrs.bindHtmlCompile);
}, function (value) {
// In case value is a TrustedValueHolderType, sometimes it
// needs to be explicitly called into a string in order to
// get the HTML string.
element.html(value && value.toString());
// If scope is provided use it, otherwise use parent scope
var compileScope = scope;
if (attrs.bindHtmlScope) {
compileScope = scope.$eval(attrs.bindHtmlScope);
}
$compile(element.contents())(compileScope);
});
}
};
}]);
<p ng-bind-html-compile="getId(xxx)"></p>