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的不确定。我想这是因为路角度进程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?
推荐答案
的元素
传递到你的链接变量
函数是一个jqLite对象 - 不是一个DOM对象。您可以获取与DOM对象元素[0]
(就像你在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屋!
查看全文