我写了有一个指令postLink
,我整理了一些HTML代码和angularjs渲染。 但问题是我无法从生成的DOM获取的HTML代码。
我的代码是:
app.directive("showResultAsText", ['$compile', function ($compile) {
return {
restrict: 'A',
compile: function compile(tElement, tAttrs, transclude) {
console.log('compile');
var watchModal = tAttrs["showResultAsText"];
var elem = jQuery(tElement);
var oriHtml = elem.html();
elem.empty();
return {
post: function postLink(scope, iElement, iAttrs, controller) {
scope.$watch(function () {
return scope.$eval(watchModal);
}, function (newVal) {
if (newVal) {
var s = $compile(angular.element(oriHtml))(scope);
console.log(s);
console.log(s[0]);
console.log(s[0].outerHTML);
console.log($('<div>').append(s[0]).html());
iElement.text(s[0].outerHTML);
}
});
}
}
}
}
}
你可以看到我用console.log
打印的价值s
。 并在控制台,它输出:
你可以看到console.log(s)
和console.log(s[0])
有很多的信息,但是当我使用outerHTML
,内按钮全部丢失。
我也试着使用jquery: jQuery(s[0]).html()
但同样的事情发生。
什么是转换的正确方法s
到HTML代码?