我在为自己寻找解决同一问题的方法时遇到了您的帖子。我使用了基于多个帖子的指令来组合以下解决方案。您可以在这里尝试(尝试调整浏览器窗口的大小):http: //jsfiddle.net/zbjLh/2/
视图:
<div ng-app="miniapp" ng-controller="AppController" ng- resize> window.height: {{windowHeight}} <br /> window.width: {{windowWidth}} <br /></div>控制器:
var app = angular.module('miniapp', []);function AppController($scope) { }app.directive('resize', function ($window) { return function (scope, element) { var w = angular.element($window); scope.getWindowDimensions = function () { return { 'h': w.height(), 'w': w.width() }; }; scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) { scope.windowHeight = newValue.h; scope.windowWidth = newValue.w; scope.style = function () { return { 'height': (newValue.h - 100) + 'px', 'width': (newValue.w - 100) + 'px' }; }; }, true); w.bind('resize', function () { scope.$apply(); }); }})仅供参考,我最初是在控制器(http://jsfiddle.net/zbjLh/)中工作的,但是从后来的阅读中发现,从Angular的角度来看,这是不明智的,因此现在我将其转换为使用指令。
重要的是,请注意
true“watch”函数末尾的标志,用于比较getWindowDimensions返回对象的相等性(如果不使用对象,则删除或更改为false)。



