我有一个必须以横向查看的网络应用程序。
为此,我创建了一个检查innerWidth
and的函数,innderHeight
如果宽度大于高度,那么快乐的日子。这在我加载页面时非常有效,但我也可以在resize
事件触发时检查方向。
所以我的代码流程是 -
- 在触发
resize
事件调用时$scope.getOrienttion()
- 计算当前方向并返回结果
- 监视
$scope.getOrienttion()
使用 a的值的变化watch
并更新 的值$scope.orientation
上面的第 1 步和第 2 步似乎工作正常,但我watch
从未检测到更改,$scope.getOrienttion()
并且仅在页面加载时触发。我一定是做错了什么,谁能帮我找出问题所在。
这是相关的AngularJS -
christmasApp.controller('bodyCtrl', function($scope, $window){
angular.element($window).bind('resize', function(){
console.log('Event triggered');
$scope.getOrientation();
});
$scope.getOrientation = function(){
var w = $window.innerWidth,
h = $window.innerHeight;
var orientation = (w > h) ? 'landscape' : 'portrait'
console.log('Function triggered - ' + orientation)
return (w > h) ? 'landscape' : 'portrait';
};
$scope.$watch($scope.getOrientation, function(newValue, oldValue){
$scope.orientation = newValue;
console.log('Watch triggered - ' + newValue);
}, true);
});
这是具有条件类集的 HTML,具体取决于$scope.orientation
(可能不相关,但以防万一)的值 -
<body <?php body_class(); ?> data-ng-controller="bodyCtrl">
<div id="orientationMask" data-ng-class="{visible: orientation != 'landscape'}">
<p>Please turn your device to the <b>landscape</b> orientation.</p>
</div>
{ Additional code, obscured by the mask if it is show... }
</body>