(function() { angular.module('Navigation') .controller('NavigationController', [ '$scope', '$state', '$menu', '$sidenav', '$location', function( $scope, $state, $menu, $sidenav, $location) { $scope.toggleSideNav = $sidenav.toggle; $scope.menuItems = $menu.items; $scope.navigate = function(menuItem) { //reset the path or ui router will favor path over our navigation $location.path('/#/dashboard'); $scope.currentMenuItem = menuItem; $state.go(menuItem.state); } $scope.getMenuClass = function(menuItem) { if (menuItem.state == $scope.currentMenuItem.state) { return 'md-focused'; } else { return ''; } }; var findMenuItemByState = function(state) { var result; $menu.items.forEach(function(item) { if (item.state == state.name) { result = item; } }); return result; }; var findStateByUrl = function(url) { var result; $state.get().forEach(function(state) { if (state.url == url) { result = state } }); return result; }; var init = function() { //find a state by URL var state = findStateByUrl($location.path()); //if found, find it's menu item and set it to current if (state != null) { $scope.currentMenuItem = findMenuItemByState(state); }; //if there is no current set, default to 0 if ($scope.currentMenuItem == null) { $scope.currentMenuItem = $menu.items[0]; // default redirect path $location.path('/' + $menu.items[0].state); } if ($scope.currentMenuItem == null) { $scope.navigate($scope.currentMenuItem); } } init(); } ]); }).call(this);