Ami*_*ava 95 angularjs angularjs-directive
我有一个元素,我想将HTML绑定到它.
<div ng-bind-html="details" upper></div>
这样可行.现在,除此之外,我还有一个绑定到绑定html的指令:
$scope.details = 'Success! <a href="#/details/12" upper>details</a>'
但是upperdiv和锚的指令没有评估.我如何使其工作?
小智 186
我也遇到了这个问题,在网上搜索了几个小时后,我读了@ Chandermani的评论,这被证明是解决方案.您需要使用此模式调用'compile'指令:
<div compile="details"></div>
Run Code Online (Sandbox Code Playgroud)
.directive('compile', ['$compile', function ($compile) {
return function(scope, element, attrs) {
scope.$watch(
function(scope) {
// watch the 'compile' expression for changes
return scope.$eval(attrs.compile);
},
function(value) {
// when the 'compile' expression changes
// assign it into the current DOM
element.html(value);
// compile the new DOM and link it to the current
// scope.
// NOTE: we only compile .childNodes so that
// we don't get into infinite loop compiling ourselves
$compile(element.contents())(scope);
}
);
};
}])
Run Code Online (Sandbox Code Playgroud)
小智 34
感谢vkammerer的好回答.我建议的一个优化是在编译运行一次后不看.watch表达式中的$ eval可能会影响性能.
angular.module('vkApp')
.directive('compile', ['$compile', function ($compile) {
return function(scope, element, attrs) {
var ensureCompileRunsOnce = scope.$watch(
function(scope) {
// watch the 'compile' expression for changes
return scope.$eval(attrs.compile);
},
function(value) {
// when the 'compile' expression changes
// assign it into the current DOM
element.html(value);
// compile the new DOM and link it to the current
// scope.
// NOTE: we only compile .childNodes so that
// we don't get into infinite loop compiling ourselves
$compile(element.contents())(scope);
// Use un-watch feature to ensure compilation happens only once.
ensureCompileRunsOnce();
}
);
};
}]);
Run Code Online (Sandbox Code Playgroud)
Joë*_*oël 27
添加此指令angular-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) {
// Incase 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);
});
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
<div bind-html-compile="data.content"></div>
Run Code Online (Sandbox Code Playgroud)
真的很容易:)
小智 13
不幸的是,我没有足够的声誉来评论.
我无法让这个工作多年.我修改了我的ng-bind-html代码以使用此自定义指令,但我无法删除$scope.html = $sce.trustAsHtml($scope.html)ng-bind-html工作所需的代码.一旦我删除它,编译功能就开始工作了.
对于任何处理已经在$sce.trustAsHtml这里运行的内容的人来说,我必须采取不同的方式
function(scope, element, attrs) {
var ensureCompileRunsOnce = scope.$watch(function(scope) {
return $sce.parseAsHtml(attrs.compile)(scope);
},
function(value) {
// when the parsed expression changes assign it into the current DOM
element.html(value);
// compile the new DOM and link it to the current scope.
$compile(element.contents())(scope);
// Use un-watch feature to ensure compilation happens only once.
ensureCompileRunsOnce();
});
}
Run Code Online (Sandbox Code Playgroud)
这只是link指令的一部分,因为我使用的是不同的布局.您还需要注入$sce服务$compile.
| 归档时间: |
|
| 查看次数: |
54804 次 |
| 最近记录: |