angularjs - How to load different html depending on screen width? -
i want change sitetype
value depending on screen width without editing in view part.
app.js
'use strict'; var app = angular.module('location', []). config(['$routeprovider', function ($routeprovider) { $routeprovider. when('/', { templateurl: 'pages/' + **params.sitetype** + '/locationlist.html', controller: location }). when('/locationdetail/:projectid', { templateurl: function (params) { return 'pages/' + **params.sitetype** + '/locationdetail.html'; }, controller: location }). otherwise({ redirectto: '/' }); }]) app.config(['$locationprovider', function($location) { $location.hashprefix('!'); }]);
controller.js
'use strict'; function location($scope, $http, $routeparams) { $scope.projectid = $routeparams.projectid; $scope.selectedproject = null; $scope.locationlist = null; $scope.sitetype = "desktop"; $(window).resize(function(){ if(window.innerwidth < 600) { $scope.$apply(function(){ $scope.sitetype = "mobile"; }); } else { $scope.$apply(function(){ $scope.sitetype = "desktop"; }); } }); $http.get("location.json") .success(function(data){ $scope.locationlist = data; var indexedloc = []; $scope.locationlisttofilter = function(){ indexedloc = []; return $scope.locationlist; } $scope.filterlocation = function(loc){ var locationisnew = indexedloc.indexof(loc.field_data_field_location_field_location) == -1; if(locationisnew){ indexedloc.push(loc.field_data_field_location_field_location); } return locationisnew; } $scope.returnfilterloc = function(){return indexedloc}; if($scope.projectid && $scope.projectid != null) { for(var = 0;i < $scope.locationlist.length;i++){ if($scope.locationlist[i].tid == $scope.projectid) { $scope.selectedproject = $scope.locationlist[i]; } } } }) .error(function(data) { $("div.category-wrapper").html("error"); }); }
you can have directive or controller sets value of included:
e.g.
<div ng-controller="mycontroller"> <div ng-include="{{ template }}"></div> </div>
and on controller can set value of template depending on window or document
$(window).height(); // returns height of browser viewport $(document).height(); // returns height of html document $(window).width(); // returns width of browser viewport $(document).width(); // returns width of html document
e.g.
myapp.controller('mycontroller', ['$scope', function($scope) { if($(window).width() < 1199) $scope.template = "mytemplate1.html" else $scope.template = "mytemplate2.html" }]);
but above messy, not safe, ugly solution. should handle responsiveness using css media-queries.
Comments
Post a Comment