index.js
angular.module('bbsApp', ['ngCommon', 'ui.router']) /** * 通过html5中history Api实现 */ .controller('MainCtrl1', ['$scope', '$apis','$location','$window',function ($scope,$apis,$location,$window) { $scope.currentInfo={ }; $apis.get('/demo/data.json',function(data){ $scope.data=data; $scope.currentInfo.city=$scope.data[0]['city']; $scope.currentInfo.county=$scope.data[0]['county']; }); //切换 $scope.changeCity=function(city){ $scope.currentInfo.city=city; $scope.currentInfo.county=getCounty(city); //历史记录管理 $location.url($scope.currentInfo.city); $location.replace(); $window.history.pushState(JSON.stringify($scope.currentInfo), null, $location.absUrl()); }; $window.onpopstate=function(event){ if(event.state){ $scope.currentInfo=JSON.parse(event.state); }else{ $scope.currentInfo.city=$scope.data[0]['city']; $scope.currentInfo.county=$scope.data[0]['county']; } }; function getCounty(city){ return $scope.data.filter(function(item){ return item.city===city; })[0]['county']; } }]) /** * 通过hash实现 */ .controller('MainCtrl', ['$scope', '$apis','$location','$window',function ($scope,$apis,$location,$window) { $scope.currentInfo={ }; $apis.get('/demo/data.json',function(data){ $scope.data=data; $scope.currentInfo.city=$scope.data[0]['city']; $scope.currentInfo.county=$scope.data[0]['county']; }); $scope.changeCity=function(city){ $scope.currentInfo.city=city; $scope.currentInfo.county=getCounty(city); }; $window.addEventListener("hashchange", function(){ var hash=decodeURIComponent(location.hash.slice(2)); if(hash){ $scope.currentInfo.city=hash; $scope.currentInfo.county=getCounty(hash); }else{ $scope.currentInfo.city=$scope.data[0]['city']; $scope.currentInfo.county=$scope.data[0]['county']; } }, false); function getCounty(city){ return $scope.data.filter(function(item){ return item.city===city; })[0]['county']; } }]); data.json [ { "city":"上海", "county":["浦东","金山","崇明","奉贤"] }, { "city":"江苏", "county":["南京","苏州","泰州","扬州"] }, { "city":"浙江", "county":["杭州","嘉兴","舟山","绍兴"] } ] index.css .column{ display: block; height: 40px; line-height: 40px; text-align: center; border-bottom: 1px solid #ccc; cursor: pointer; } .city{ border: 1px solid #ccc; border-right: none; } .county{ width: 400px; border: 1px solid #ccc; height: 300px; } .active{ color: white; background: #1daace; }