如何在 angular js 中获取 window 高度

How to get window height in angular js

我使用 iframe 来显示使用 angularjs 和离子框架的内容。在这里,我需要给 window 高度作为 iframe 高度,所以我使用了

  $scope.iframeHeight = window.innerHeight;

但是,我只有 1/4 屏幕。

这是我到目前为止所尝试的。

index.html

<!DOCTYPE html>
<html ng-app="ionicApp">
    <head>
    <!-- ionic/angularjs CSS -->
    <link href="css/ionic.css" rel="stylesheet">
    <link href="css/ionic-custom.css" rel="stylesheet">
    <!-- ionic/angularjs js bundle -->
    <script type="text/javascript" src="js/ionic.bundle.js"></script>
    <script>
    angular.module('ionicApp', ['ionic'])
.controller('MainCtrl', function($scope) { 
  $scope.iframeHeight = window.innerHeight;
});
    </script>
    </head>
    <body ng-controller="MainCtrl">
        <iframe src="http://whosebug.com" ng-style="{height:iFrameHeight}" id="iframeid" width="100%" ></iframe>   
    </body>
</html>

我是不是漏掉了什么?

由于 AngularJS 本身包含 jQuery 的一小部分,您可以使用:

// Returns height of browser viewport
$scope.iframeHeight = $(window).height();

// Returns height of HTML document
$scope.iframeHeight = $(document).height();

主要问题:您需要将 'px' 个单位添加到 window.innerHeight 个数字。第二个,变量名是 iframeHeight 而不是 iFrameHeight。固定表达式将如下所示:

ng-style="{height: iframeHeight + 'px'}"

演示: http://plnkr.co/edit/NdQB5afQT7kMkf27k7wg?p=preview

window 对象,虽然在全球范围内可用,但在 angular 文档 https://docs.angularjs.org/api/ng/service/$window 中作为参考存在可测试性问题。看起来这不是您的问题虽然是代码,但为了良好实践,您可以注入 $window 服务,然后引用该服务,当然还有底部的 "px" 问题。

   angular.module('ionicApp', ['ionic']) 
    .controller('MainCtrl', function($scope, $window) { 
   $scope.iframeHeight = $window.innerHeight;
     });

稍后如前所述;

  ng-style="{height: iframeHeight + 'px'}"

使用angular

计算windows身高
$scope.screenHeight = '';
$scope.calculateoriginalWidth = function() {
    var width = $window.innerWidth;
    $rootScope.originalWidth = width;
}
$scope.calculateScreenHeight = function() {
    var height = $window.innerHeight;
    var width = $window.innerWidth;
    var subheight = 0;
    var headerheight = document.getElementById('headerTitle1');
    var headerheight2 = document.getElementById('headerTitle2');
    if (headerheight) {
        subheight += headerheight.offsetHeight;
    }
    if (headerheight2) {
        subheight += headerheight2.offsetHeight;
    }

    $scope.screenHeight = height - subheight - 20;
    $rootScope.screenHeight = $scope.screenHeight;
    $scope.screenWidth = width;
    $rootScope.screenWidth = $scope.screenWidth;
}

var reg = $scope.$on('screenResize', function($evt, args) {
    $scope.calculateScreenHeight();
    $scope.$apply();
})

window.onresize = function(event) {
    $scope.$apply(function() {
    $scope.calculateScreenHeight();
    })
};

$scope.calculateScreenHeight();
$scope.calculateoriginalWidth();

您需要在angular中添加所需的依赖项才能使用此代码。希望这将帮助您计算 window.

的高度