2014-10-02 62 views
1

的高度,我有以下DOM:訪問編譯的transcluded範圍來計算元素

<carousel calculate-carousel-height> 
    <slide ng-repeat="article in bestArticles" active="slide.active"> 
     <div>{{article.text}}</div> 
    </slide> 
</carousel> 

carousel指令使用transcluded範圍,所以我可以在我的自定義指令calculate-carousel-height的鏈接功能得到它:

link: function (scope, element, attributes, ctrl, transcludeFn) { 
    transcludeFn(function(clonedTranscludedContent) { 
     // here I can access the transcluded scope 
    }); 
} 

但是,問題是這個範圍還沒有編譯(ng-repeat指令還沒有被處理),所以我沒有article.text並且不能計算包含文本的DOM的高度。如何以及何時可以在編譯完成後訪問transcluded作用域?

這將工作,但它是最好的解決方案?

link: function (scope, element, attributes, ctrl, transcludeFn) { 
    setTimeout(function() { 
     // angular finished its magic and I can access compiled transcluded DOM here 
    }, 0); 
} 

回答

0

我有一個類似的問題與ng-repeat,這是我的固定它:

link: function(scope, element, attrs) { 
    scope.$watch('article.text', function(text) { 
     console.log(text); 
    } 
} 

我不知道在這種情況下,transclude功能,也許這不是正確的回答,但它可能會指出你正確的方向。