我想设置元素的CSS属性顶部基地的高度。要做到这一点我创建这样的指令:如何访问Angular指令中的元素?
directive('errormsgbox', function($timeout) {
return {
restrict: 'EA',
scope: false,
replace:true,
templateUrl: 'submitter/reports/errormsgbox.html',
link: function(scope,element) {
$timeout(function(){
$timeout(function(){
console.log(element[0].offsetHeight);
},2000);
},2000)
}
};
})
指令HTML:
<span ng-if='expenses.hasBlockers || expenses.hasWarnigs' style='border:1px solid gray' ng-show='policyViolation && showpencil' class='msgbox'>
<ul>
<li ng-repeat='policymsg in expenses.policyMsg'>
<span class='floatleft' ng-class="showPolicyClass(policymsg.type)">{{policymsg.type}} - </span><span style='width:285px;' class='floatleft'>{{policymsg.message}}</span>
<div class='clear'></div>
</li>
</ul>
</span>
主HTML:
<td class="text-center" style='position:relative'>
<errormsgbox></errormsgbox>
</td>// it is in a table cell, the content of directive is from ng-repeat in this table
我需要访问<span>
。但是你可以看到,指令工作正常,它会显示正确的信息,但每次它都会记录元素高度undefined
。任何想法如何访问此?在开发者控制台
angular.element($0).scope()
:
哇,感谢分享'$ 0'功能。 –
很酷的功能,但这不一定回答 – scniro
谢谢你们的问题,但如何解决我的问题.... – linyuanxie