Skip to content

Instantly share code, notes, and snippets.

@matthewcopeland
Created September 23, 2015 20:23
Show Gist options
  • Save matthewcopeland/db60ca324801aac36318 to your computer and use it in GitHub Desktop.
Save matthewcopeland/db60ca324801aac36318 to your computer and use it in GitHub Desktop.
Using local storage to maintain navigation collapse state.
angular.module("app.controllers")
.controller("NavCtrl", [
"$scope",
"$window",
function($scope, $window) {
$scope.toggleCollapse = function() {
$scope.collapsed = !$scope.collapsed;
setCollapsed($scope.collapsed);
};
var store = $window.localStorage;
var key = 'header-collapsed';
function init() {
if( store.getItem(key) ) {
$scope.collapsed = true;
} else {
$scope.collapsed = false;
}
}
function setCollapsed(setting) {
if (setting) {
store.setItem(key, setting);
} else {
store.removeItem(key);
}
}
// Initialize
init();
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment