Javascript AngularJS-从指令访问DOM元素
我正在使用带有templateURL的指令:Javascript AngularJS-从指令访问DOM元素,javascript,angularjs,Javascript,Angularjs,我正在使用带有templateURL的指令: function myDirective() { return { restrict: 'E', replace: false, link: function (scope, element, attrs) { element[0].style.height = $(window).height() + 'px'; }, templateUrl: chrome.extension.getURL(
function myDirective() {
return {
restrict: 'E',
replace: false,
link: function (scope, element, attrs) {
element[0].style.height = $(window).height() + 'px';
},
templateUrl: chrome.extension.getURL('./templates/hn_feed_tpl.html')
}
}
在我的html中,我插入如下指令:
<my-directive></my-directive>
加载的模板非常简单:
<div class="myDiv">
// some code here
</div>
//这里有一些代码
我想从指令代码访问.myDiv
div来调整其高度。但是,当我尝试使用元素[0]
对其进行调整时,所选元素为
。如何访问.myDiv
div
非常感谢您可以通过类选择器选择儿童:
element.children(".myDiv")
当您将
replace
设置为false
时,您的指令将呈现为
<my-directive>
<div class="myDiv">
// some code here
</div>
</my-directive>
将replace
设置为true
可以避免模板元素的包装,只需将指令元素替换为模板中定义的元素即可。在这种情况下,您可以直接使用
element
,因为它指向模板中定义的元素。element.children()。。。最好记住:指令定义中的replace属性在1.3.x中已被弃用
element.children()[0].style.height = whatever