如何获得角度js的窗口高度

时间:2015-02-25 07:36:57

标签: javascript html angularjs iframe ionic-framework

我使用iframe来使用angularjs和离子框架显示内容。在这里,我需要将窗口高度设为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://stackoverflow.com" ng-style="{height:iFrameHeight}" id="iframeid" width="100%" ></iframe>   
    </body>
</html>

我错过了什么吗?

4 个答案:

答案 0 :(得分:11)

主要问题:您需要将“px”单位添加到window.innerHeight个数字。第二个,变量名是iframeHeight而不是iFrameHeight。修正后的表达式如下:

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

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

答案 1 :(得分:6)

尽管全局可用,但是窗口对象在angular docs 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'}"

答案 2 :(得分:5)

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

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

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

答案 3 :(得分:0)

使用角度

计算窗户高度
$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();

您需要在角度中添加所需的依赖项才能使用此代码。希望这可以帮助您计算窗口的高度。