对于AngularJS应用程序中的任何给定路由或状态,我希望根据用户的设备使用特定于移动设备或桌面的模板。我也希望用户能够选择哪个版本(桌面或移动版),而不管他们在哪个设备上。AngularJS:为桌面和移动使用不同的模板?
具体而言,我想要一个单独的模板,因为在很多情况下,单独的模板比使用响应式设计要简单得多。
有没有简单的方法来实现这个目标?
这是我想要做的,但我不知道如何使用angular-ui-router为templateUrl函数创建'isMobile'变量。任何帮助将不胜感激。
angular
.module('app.items', [])
.config(config);
function config($stateProvider) {
//$window service not available in config block, so how can I set this variable?
var isMobile = $window.sessionStorage.getItem('isMobile');
$stateProvider
.state('itemsList', {
url: '/items',
templateUrl: function($stateParams) {
if (isMobile) { //How do I get this information (variable) in here?
return 'app/items/list.mobile.html';
}
else {
return 'app/items/list.desktop.html'
}
},
controller: 'ItemsListController'
});
}
'isMobile'在'templateUrl'函数内部可用。你有没有尝试运行你的代码? – dfsq
我知道它是成功地从上面引用var isMobile,但是如何从'window.sessionStorage'这样的'global'位置设置它? –