我正在嘗試構建一個自定義指令,將其內容重新排列爲網格。 我想跨越ng-repeat
指令的結果,然後重新排列結果元素。如何獲得我自定義指令中的ng-repeat子元素的句柄?
問題是當我在鏈接函數中調用element.children()
方法時,我有一個空數組,因爲ng-repeat
指令尚未呈現並被解釋爲註釋。
否則,如果指令的內容爲「靜態」,則該指令工作得很好。
的HTML
<grid n='6'>
<div ng-repeat="i in [1,2,3]"></div>
</grid>
我只用代碼的有趣的作品指令:
app.directive('grid', [function() {
return {
restrict: 'E',
replace: true,
transclude: true,
template: "<div ng-transclude></div>",
link: function (scope, grid, attrs) {
// With an ngRepeat transcluded, els result in an empty array
var els = grid.children();
// ...
};
}]);
我缺少什麼?
在你的鏈接功能,你可以嘗試使用$ timout(函數(){//接入電網兒童},0); – Chandermani
確實有效,但這種方法可靠嗎? –
其實你的子模板在post鏈接函數後得到渲染,所以你需要添加$ timout,它會起作用,因爲它會引入一些延遲。 – Chandermani