2016-01-22 97 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也是另一篇讨论它的文章。