在模板中使用AngularJS依赖项

时间:2014-09-10 14:53:02

标签: angularjs

我想将max-height的{​​{1}}设为div。以下代码失败:

$window.innerHeight

我的走动是使用范围变量:

<div ng-style="{'max-height': $window.innerHeight}" style="overflow-y: auto">

有没有办法实现$scope.divnHeight = $window.innerHeight; <div ng-style="{'max-height': divHeight}" style="overflow-y: auto"> ng-style绑定?

谢谢

1 个答案:

答案 0 :(得分:1)

你错过了ng风格的'px'

请参阅Plunker:http://plnkr.co/edit/eqKz9wgyzm8LOz6pZAN0?p=preview

要回答您的问题,请不要......您希望在HTML中使用的任何变量都必须是范围变量。

如果窗口高度发生变化,那么您可以使用观察者将变量绑定到窗口高度:

$scope.$watch(function(){
   return $window.innerHeight; // the thing to watch
}, function(newValue, oldValue) { // the function to run when the value changes
   console.log(newValue);
   $scope.divnHeight = $window.innerHeight;
});

更新

尝试将此添加到您的控制器中,这只是在您调整大小时注册要触发的函数:

window.onresize = function(event) {
    console.log("Fire resize");
    $scope.divnHeight = $window.innerHeight;
};

此外,尺寸可能保持不变,因为您使用的是最大高度而不是高度,在这种情况下,高度取决于内容,请尝试使用高度并查看是否会产生影响:

<div ng-style="{'height': divHeight + 'px'}" style="overflow-y: auto">