2013-10-24 79 views
2

我使用這個指令:如何使角度指令不斷檢查元素的高度?

App 
    .directive('maxHeightBorder', function(){ 
    return { 
     restrict: 'A', 
     link: function($scope, $el, $attr){ 
      if($el.height() >= $attr.maxHeightBorder){ 
       $el.css('box-shadow','0 0 5px 5px black'); 
      } 
     } 
    } 
}); 

與此元素:<ul class="list-group" id="events-list" max-height-border="250">

問題是:當我加載頁面,這個元素是空的,指令的if語句失敗。加載之後,Angular填充這個元素的高度超過250px,但是這個指令不會被檢查,所以新的樣式從不應用。

+0

你如何填充內容的元素?請在代碼中顯示更多的代碼 – subZero

+0

ul裏面的trackedEvents'>裏面的ng-repeat ='trackedEvent' – Cystack

回答

1

試試這個:

directive('maxHeightBorder', function($timeout){ // inject $timeout wrapper 
    return { 
     restrict: 'A', 
     link: function(scope, el, attr){ 
      $timeout(function(){ 
       if(el.height() >= attr.maxHeightBorder){ 
        el.css('border-bottom','2px solid'); 
       } 
      }); 
     } 
    } 
}); 

參見:How JavaScript Timers Work

+0

定時器將成爲回退解決方案如果一切都失敗。這是一個可能經常出現的條件CSS,我不想在幾十個併發定時器下下載。還是)感謝你的建議! – Cystack

+0

然後,您可以廣播/發出自定義事件並檢查這些事件。請參閱:http://docs.angularjs.org/api/ng.$ro​​otScope.Scope – subZero