javascript 如何使用 AngularJS 设备检测器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26836783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to device detector use AngularJS
提问by NANA
I am building a mobile site using the ionic framework. I want to detect mobile devices(Android and iOS) with AngularJS (or ionic).
我正在使用离子框架构建一个移动站点。我想用 AngularJS(或 ionic)检测移动设备(Android 和 iOS)。
If access device is Android → #/android if access device is iOS → #/ios
如果接入设备是Android → #/android 如果接入设备是iOS → #/ios
controllers.js
控制器.js
function uaCtrl('$scope', '$location', ($scope, $location) {
$scope = function () {
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone | iPad | iPod/i)
}
}
if(isMobile.Android()) {
$location.path('#/android');
}else if(isMobile.iOS()) {
$location.path('#/ios');
}else{
$location.path('#/ios');
}
};
};
};
app.js
应用程序.js
.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/dash');
$stateProvider
.state('home', {
url: '/dash',
templateUrl: 'templates/dash.html'
})
.state('android-home', {
url: '/android',
templateUrl:'templates/dash-android.html'
})
.state('ios-home', {
url: '/ios',
templateUrl:'templates/dash-ios.html'
})
});
dash.html
破折号.html
<ion-view hide-nav-bar="true" ng-controller="uaCtrl">
?????
</ion-view>
回答by adam
ionic.Platform.isIOS()
I believe is what you're looking for.
ionic.Platform.isIOS()
我相信这就是你要找的。
Platform docs: http://ionicframework.com/docs/api/utility/ionic.Platform/
平台文档:http: //ionicframework.com/docs/api/utility/ionic.Platform/
It's unit tests: https://github.com/driftyco/ionic/blob/master/test/unit/angular/service/platform.unit.js
这是单元测试:https: //github.com/driftyco/ionic/blob/master/test/unit/angular/service/platform.unit.js
However, I wouldn't recommend using different markup structures if you don't have to. Instead, if you have different styles depending on the platform, Ionic places platform-android
or platform-ios
in the body tag class, so you could do things like:
但是,如果您不需要,我不建议您使用不同的标记结构。相反,如果您根据平台、Ionic 位置platform-android
或platform-ios
body 标签类具有不同的样式,那么您可以执行以下操作:
.platform-android h1 { color: green; }
.platform-android h1 { color: green; }