ui-router实现返回上一页功能
angular.module('ConsoleUIApp', ['ui.router','ui.bootstrap']) .config(function ($stateProvider, $urlRouterProvider, $httpProvider) { // For any unmatched url, redirect to /state1 $urlRouterProvider.otherwise("/home"); $stateProvider .state('home', { url: "/home", templateUrl: "views/home.html", controller: 'HomeCtrl' }) }) .run(function($rootScope, $state, $stateParams) { // previous state handling $rootScope.previousState = {}; $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { // store previous state in $rootScope $rootScope.previousState.name = fromState.name; $rootScope.previousState.params = fromParams; }); });
back-button
directive :
yourModule.directive('backButton', [ '$rootScope', '$state', '$parse', function($rootScope, $state, $parse) { return { restrict: 'EA', link: function(scope, el, attrs) { var defaultState , defaultStateParams; el.click(function() { var stateName , stateParams; if ($rootScope.previousState.name) { stateName = $rootScope.previousState.name; stateParams = $rootScope.previousState.params; } else { stateName = defaultState; stateParams = defaultStateParams; } if (stateName) $state.go(stateName, stateParams); }); attrs.$observe('defaultState', function() { defaultState = attrs.defaultState; }); attrs.$observe('defaultStateParams', function() { defaultStateParams = $parse(attrs.defaultStateParams)(scope); }); $rootScope.$watch('previousState', function(val) { el.attr('disabled', !val.name && !defaultState); }); } }; }]);
https://github.com/angular-ui/ui-router/issues/92