【发布时间】:2016-01-20 13:49:01
【问题描述】:
我是 angularJS 的新手,学习指令(并且非常努力:))。
我正在尝试理解plunker 中的一段 angularJS 代码 由用户 tasseKATT 为 stack overflow question 提供有关 angular-ui-bootstrap 的信息。
我希望有人能更详细地解释这个代码片段。
具体
还有什么是
(value || '').toString();
用于。
指令的片段如下
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);
});
};
}
};
}
]);
【问题讨论】:
标签: angularjs angularjs-directive