I've got a seemingly simple problem with no apparent (by reading the Angular JS docs) solution.

我有一个Angular JS指令,它根据其他DOM元素的高度进行一些计算,以定义DOM中容器的高度.

指令中也有类似的情况:

return function(scope, element, attrs) {
    $('.main').height( $('.site-header').height() -  $('.site-footer').height() );
}

问题是,当指令运行时,找不到$('site-header'),返回一个空数组,而不是我需要的jQuery包装的DOM元素.

Is there a callback that I can use within my directive that only runs after the DOM has been loaded and I can access other DOM elements via the normal jQuery selector style queries?

推荐答案

It depends on how your $('site-header') is constructed.

You can try to use $timeout with 0 delay. Something like:

return function(scope, element, attrs) {
    $timeout(function(){
        $('.main').height( $('.site-header').height() -  $('.site-footer').height() );
    });        
}

Explanations how it works: one, two.

别忘了在您的指令中注入$timeout:

.directive('sticky', function($timeout)

Jquery相关问答推荐

提交表单时运行自定义验证脚本

使用 howler.js 中的变量播放多种声音

在 JQuery DataTable 中通过按钮单击传递 ID

更改后如何使用 jQuery Select CSS 类

javascript:无效(0); vs 返回 false vs preventDefault()

jQuery UI 自动完成宽度未正确设置

在不刷新页面的情况下更新网站的 CSS

在不使用提交按钮的情况下触发标准 HTML5 验证(表单)?

根据弹出框相对于窗口边缘的 X 位置更改 Bootstrap 弹出框的位置?

如何在 JavaScript 或 jQuery 中规范化 HTML?

jQuery ajax 在 asp.net mvc 中上传文件

未捕获的 TypeError:data.push 不是函数

在 jQuery UI 自动完成上没有检测到结果

无法更新数据属性值

AngularJS:使用 jQuery 更改时,ng-model 绑定不更新

通过 AJAX MVC 下载 Excel 文件

你如何记录jQuery中一个元素触发的所有事件?

是否可以将 async:false 设置为 $.getJSON 调用

jQuery向左滑动并显示

使用 jQuery 将一个标签替换为另一个标签