2016-01-22 116 views
0

我有一個狀態可以有一個可變的高度(取決於內容)。我想將該高度設置爲我的背景顏色的CSS屬性。獲取ui-view中元素的高度

但我無法獲得正確的高度,因爲代碼在內容加載之前觸發狀態。

我有這樣的狀態,

$stateProvider 
    .state('home.suggestions', { 
    url: '', 
    views: { 
     "suggestions": { 
     templateUrl: '../assets/angular-app/templates/_suggestions.html', 
     controller: function(){ 
      showHeight = $('.suggestions-container-wrapper').outerHeight(); 
      console.log(showHeight) 
     } 
     }, 

    }, 
    }) 

但它加載ng-repeat內容之前,它始終返回175(元素的高度。

如何運行的代碼後,所有的呼叫都做了什麼?

+0

我可能會爲此創建一個指令並將其放在模板的頂層 – aw04

回答

0

剛剛發現一個簡單的解決方案,但一個ng-init的NG-repeat元素後,則觸發該功能時,它得到triggerd。

https://stackoverflow.com/a/24817862/848706

%div{"ng-init" => "setHeight()"} 

控制器,

$scope.setHeight = function(){ 
    $timeout(function(){ 
     showHeight = $('.suggestions-container-wrapper').outerHeight(); 
     console.log(showHeight) 
    }, 0); 

} 
0

檢查出$viewContentLoaded的文檔。您可能可以這樣做:

controller: function(){ 
    $scope.$on('$viewContentLoaded', function(){ 
     showHeight = $('.suggestions-container-wrapper').outerHeight(); 
     console.log(showHeight) 
    }); 
} 

Here也是另一篇討論它的文章。