Javascript .html()返回空字符串
我有密码:Javascript .html()返回空字符串,javascript,html,angularjs,angularjs-directive,Javascript,Html,Angularjs,Angularjs Directive,我有密码: htmlObject = '<div status-box></div>'; template = $compile(htmlObject)($scope); $scope.$digest(); console.log(template); var templateAsHtml = template.html(); console.log(templateAsHtm
htmlObject = '<div status-box></div>';
template = $compile(htmlObject)($scope);
$scope.$digest();
console.log(template);
var templateAsHtml = template.html();
console.log(templateAsHtml);
htmlObject='';
模板=$compile(htmlObject)($scope);
$scope.$digest();
控制台日志(模板);
var templateAsHtml=template.html();
console.log(templateAsHtml);
以及输出:
从第一个console.log:
{0:,长度:1}
从第二天开始:
”
似乎在我调用.html的那一刻,它只是没有转换它,它是空的。当你请求一个元素的
.html()
时,你会得到它的innerHTML
。由于'
没有子节点,因此行为是正确的。如果要返回包含对象本身的HTML,则需要使用outerHTML
函数
var templateAsHtml = template[0].outerHTML;
console.log(templateAsHtml);
见文件:
注意:
[0]
用于访问包装器对象内部的普通JavaScript对象。当您请求元素的.html()
时,您将获得该元素的内部html
。由于'
没有子节点,因此行为是正确的。如果要返回包含对象本身的HTML,则需要使用outerHTML
函数
var templateAsHtml = template[0].outerHTML;
console.log(templateAsHtml);
见文件:
注意:
[0]
用于访问包装器对象内部的香草JavaScript对象。var templateAsHtml=template[0].outerHTML代码>,为什么?html()获取innerHtml,因此必须使用外部的var templateAsHtml=template[0]代码>,为什么?html()获取内部html,所以必须使用外部html