を持つディレクティブを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コードに変換する正しい方法は何ですか?