Angularjs 如何在ionic framework中根据屏幕方向显示/隐藏元素?
我使用纯JS来检测设备的屏幕方向,但我不知道如何显示或隐藏取决于方向的元素。基本上与 这是模板的摘录Angularjs 如何在ionic framework中根据屏幕方向显示/隐藏元素?,angularjs,ionic-framework,Angularjs,Ionic Framework,我使用纯JS来检测设备的屏幕方向,但我不知道如何显示或隐藏取决于方向的元素。基本上与 这是模板的摘录 <img class="cover" ng-src="http://i.imgur.com/vkW3Lhe.jpg" ng-show="orientation.landscape"> <img class="cover" ng-src="http://i.imgur.com/vkW3Lhe.jpg" ng-show="orientation.portrait"> 确保
<img class="cover" ng-src="http://i.imgur.com/vkW3Lhe.jpg" ng-show="orientation.landscape">
<img class="cover" ng-src="http://i.imgur.com/vkW3Lhe.jpg" ng-show="orientation.portrait">
确保您使用,$apply
等
angular.module('starter', [
'ionic',
'ngCordova'
])
.factory('Orientation', function($ionicPlatform, $timeout){
var ret = {
LANDSCAPE: 'LANDSCAPE',
PORTRAIT: 'PORTRAIT',
current: void 0
};
function handleOrientation() {
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; // Detect Android devices
if (isAndroid) {
//window.orientation is different for iOS and Android
if (window.orientation == 0 || window.orientation == 180) { //Landscape Mode
$timeout(function(){
ret.current = ret.LANDSCAPE;
})
}
else if (window.orientation == 90 || window.orientation == -90) { //Portrait Mode
$timeout(function(){
ret.current = ret.PORTRAIT;
})
}
}
else {
if (window.orientation == 90 || window.orientation == -90) { //Landscape Mode
$timeout(function(){
ret.current = ret.LANDSCAPE;
})
}
else if (window.orientation == 0 || window.orientation == 180) { //Portrait Mode
$timeout(function(){
ret.current = ret.PORTRAIT;
})
}
}
}
$ionicPlatform.ready(function() {
window.addEventListener("orientationchange", handleOrientation);
window.addEventListener("load", handleOrientation);
})
return ret;
})
.controller('SomeController', function(Orientation, $scope) {
$scope.$watch(
function(){
return Orientation.current;
},
function(orientation){
if(orientation === Orientation.LANDSCAPE) {
}
if(orientation === Orientation.PORTRAIT) {
}
})
})
这是一个未经测试的快速答案,应该使用
$window
等进行重构。
angular.module('starter', [
'ionic',
'ngCordova'
])
.factory('Orientation', function($ionicPlatform, $timeout){
var ret = {
LANDSCAPE: 'LANDSCAPE',
PORTRAIT: 'PORTRAIT',
current: void 0
};
function handleOrientation() {
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; // Detect Android devices
if (isAndroid) {
//window.orientation is different for iOS and Android
if (window.orientation == 0 || window.orientation == 180) { //Landscape Mode
$timeout(function(){
ret.current = ret.LANDSCAPE;
})
}
else if (window.orientation == 90 || window.orientation == -90) { //Portrait Mode
$timeout(function(){
ret.current = ret.PORTRAIT;
})
}
}
else {
if (window.orientation == 90 || window.orientation == -90) { //Landscape Mode
$timeout(function(){
ret.current = ret.LANDSCAPE;
})
}
else if (window.orientation == 0 || window.orientation == 180) { //Portrait Mode
$timeout(function(){
ret.current = ret.PORTRAIT;
})
}
}
}
$ionicPlatform.ready(function() {
window.addEventListener("orientationchange", handleOrientation);
window.addEventListener("load", handleOrientation);
})
return ret;
})
.controller('SomeController', function(Orientation, $scope) {
$scope.$watch(
function(){
return Orientation.current;
},
function(orientation){
if(orientation === Orientation.LANDSCAPE) {
}
if(orientation === Orientation.PORTRAIT) {
}
})
})