我认为您忘记通过调用来触发摘要循环scope.$apply();
在......的最后scope.onResize
method
Anyways, I used following directive (took from HERE https://stackoverflow.com/questions/14703517/angular-js-set-element-height-on-page-load) that works for me:
Try to open debug view and change view height: Demo Fiddle http://jsfiddle.net/zbjLh/270/
app.directive('resize', function ($window) {
return function (scope, element, attr) {
var w = angular.element($window);
scope.$watch(function () {
return {
'h': w.height(),
'w': w.width()
};
}, function (newValue, oldValue) {
scope.windowHeight = newValue.h;
scope.windowWidth = newValue.w;
scope.resizeWithOffset = function (offsetH) {
scope.$eval(attr.notifier);
return {
'height': (newValue.h - offsetH) + 'px'
//,'width': (newValue.w - 100) + 'px'
};
};
}, true);
w.bind('resize', function () {
scope.$apply();
});
}
});
及用法:
<div ng-style="resizeWithOffset(165)"
resize
notifier="notifyServiceOnChage(params)"
>
/** ... */
</div>
虚拟控制器方法使用:
$scope.notifyServiceOnChage = function(){
console.log($scope.windowHeight);
};
[EDIT]
这是没有 jQuery 库的演示,使用innerHeight
Demo 3Fiddle http://jsfiddle.net/zbjLh/506/