Angular.js:在页面加载时设置元素高度 [英] Angular.js: set element height on page load
问题描述
我是 AngularJS 新手.我想创建高度取决于窗口高度的网格(使用 ng-grid).$('.gridStyle').height($(window).height() - 100);
我已经写了指令:
app.directive('resize', function($window) {返回函数(范围,元素){函数应用高度(){scope.height = $window.innerHeight;$('.gridStyle').height(scope.height - 100);}angular.element($window).bind('resize', function() {范围.$应用(函数(){应用高度();});});应用高度();};});
这在我调整浏览器窗口大小时效果很好,但在第一次加载网站时不会应用样式.我在哪里可以放置代码来初始化高度?
我在为自己寻找相同问题的解决方案时看到了您的帖子.我使用基于许多帖子的指令将以下解决方案放在一起.你可以在这里尝试(尝试调整浏览器窗口的大小):http://jsfiddle.net/zbjLh/2/>
查看:
window.height: {{windowHeight}}
window.width: {{windowWidth}}
控制器:
var app = angular.module('miniapp', []);功能 AppController($scope) {/* 逻辑在这里 */}app.directive('resize', function ($window) {返回函数(范围,元素){var w = angular.element($window);scope.getWindowDimensions = 函数 () {return { 'h': w.height(), 'w': w.width() };};scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {scope.windowHeight = newValue.h;scope.windowWidth = newValue.w;scope.style = 函数 () {返回 {'高度':(newValue.h - 100) + 'px','宽度':(newValue.w - 100) + 'px'};};}, 真的);w.bind('resize', function () {范围.$应用();});}})
仅供参考,我最初让它在控制器中工作(http://jsfiddle.net/zbjLh/),但后来阅读发现从 Angular 的角度来看这很不酷,所以我现在将其转换为使用指令.
重要的是,请注意 'watch' 函数末尾的 true
标志,用于比较 getWindowDimensions 返回对象的相等性(如果不使用对象,则删除或更改为 false).
I'm AngularJS newbie. I want to create grid (using ng-grid) which height depends on window height ie. $('.gridStyle').height($(window).height() - 100);
I have written directive:
app.directive('resize', function($window) {
return function(scope, element) {
function applyHeight() {
scope.height = $window.innerHeight;
$('.gridStyle').height(scope.height - 100);
}
angular.element($window).bind('resize', function() {
scope.$apply(function() {
applyHeight();
});
});
applyHeight();
};
});
This works well when I resize browser window but style is not applied when site is loaded for the first time. Where can I put code to initalize height?
I came across your post as I was looking for a solution to the same problem for myself. I put together the following solution using a directive based on a number of posts. You can try it here (try resizing the browser window): http://jsfiddle.net/zbjLh/2/
View:
<div ng-app="miniapp" ng-controller="AppController" ng-style="style()" resize>
window.height: {{windowHeight}} <br />
window.width: {{windowWidth}} <br />
</div>
Controller:
var app = angular.module('miniapp', []);
function AppController($scope) {
/* Logic goes here */
}
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();
});
}
})
FYI I originally had it working in a controller (http://jsfiddle.net/zbjLh/), but from subsequent reading found that this is uncool from Angular's perspective, so I have now converted it to use a directive.
Importantly, note the true
flag at the end of the 'watch' function, for comparing the getWindowDimensions return object's equality (remove or change to false if not using an object).
这篇关于Angular.js:在页面加载时设置元素高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!