AngularJS element.innerHTML 在指令中未定义 [英] AngularJS element.innerHTML is undefined from within directive
本文介绍了AngularJS element.innerHTML 在指令中未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有:
directives.directive('foo', function () {
return {
restrict:'A',
scope: true,
link:function (scope, element, attr) {
console.log('innerHTML is ' + element.innerHTML);
scope.$watch('update', function (newValue) {
console.log('innerHTML is... ' + element.innerHTML);
});
}
}
});
... 那么innerHTML 是未定义的.我想这是由于 Angular 处理 DOM 的方式.获取innerHTML的正确方法是什么?
... then innerHTML is undefined. I imagine this is due to the way Angular processes the DOM. What is the right way to obtain the innerHTML?
推荐答案
传递给 link
函数的 element
变量是一个 jqLite 对象 - 而不是 DOM 对象.您可以使用 element[0]
获取 DOM 对象(就像在 jQuery 中一样),但 jqLite 为您提供了一个方法:element.html()
.查看文档.
The element
variable that is passed to your link
function is a jqLite object - not a DOM object. You can obtain the DOM object with element[0]
(like you could in jQuery), but jqLite provides a method for you: element.html()
. Check out the docs.
这篇关于AngularJS element.innerHTML 在指令中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文