2016-07-14 130 views
0

我有一個這樣的嵌套指令。angularjs子指令元素選擇

<main> 
<app-header></app-header> 
<app-footer></app-footer> 
</main> 

而且app-footer指令有一個小div含量約50像素的高度。

<div class="footer"></div 

所以我想在main指令鏈接函數中獲得頁腳高度。

angular.module("app").directive("main", [function() { 

    return { 
     "restrict": "E", 
     "transclude": true, 
     "template": "<h1>hello</h1><div ng-transclude></div>", 
     "link": link 
    }; 

    function link(scope, element) { 

     var footerHeight1 = $(".footer").height() || 0; // returns 0 
     var footerHeight2 = element.find('.footer'); // returns prevObject 

    } 
}]); 

所以我不能得到頁腳的高度。

+1

**爲什麼**你想要的高度? **當你想要身高嗎?頁腳可能是動態填充的,並且在鏈接功能被觸發時尚未填充 – devqon

+0

請向我們顯示template.html – jbrown

+0

模板內容很簡單。我更新了模板內容。 – barteloma

回答

0

您可能要根據頁腳的高度計算main的邊距/填充/高度。你可以做什麼,是這樣的(頁腳高度可能會改變):

function link(scope, element) { 
    var footer = element.find(".footer"); 

    scope.$watch(function() { 
     return footer.height(); 
    }, function(value) { 
     // do something with main, here you know the height of the footer 
     // footerheight = value 
    }); 

}