充分利用AngularJS编译的HTML [英] Getting Compiled HTML from AngularJS
本文介绍了充分利用AngularJS编译的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法得到一个页面的AngularJS的编译的HTML。这里的code:
I'm having trouble getting the compiled html of a page in AngularJS. Here's the code:
JS:
<script src="http://code.angularjs.org/1.2.0rc1/angular.min.js"></script>
<script>
var app = angular.module('main', []);
app.directive("compile", ['$compile', function ($compile) {
return {
link: function(scope, elem, attr){
var compiledHTML = $compile(elem.contents())(scope);
console.log(compiledHTML);
var returnString = '';
for(i=0; i<compiledHTML.length; i++)
returnString += compiledHTML[i].outerHTML;
console.log(returnString);
}
};
}]);
</script>
HTML
<html ng-app="main" compile>
<body>
{{3 + 4}}
</body>
</html>
奇怪的是,在第一次的console.log(),它显示了汇编的数据,7,在的outerHTML属性,但是当我输出的所有.outerHTML,它显示了未编译版本,{{3 + 4} }
What is strange is in the first console.log(), it shows the compiled data, 7, in the outerHTML property, but when I output all the .outerHTML, it shows the uncompiled version, {{3 + 4}}
推荐答案
看起来就像是一个时间问题。等待处理compiledHTML奏效了。
Looks like it was a timing issue. Waiting to process the compiledHTML did the trick.
$timeout(function(){
var returnString = '';
for(i=0; i<compiledHTML.length; i++)
returnString += compiledHTML[i].outerHTML;
console.log(returnString);
},0);
这篇关于充分利用AngularJS编译的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文