• 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

  • 相关阅读:
    es5核心技术
    es6 迭代器 和 生成器 学习笔记
    nodejs 基础学习笔记
    node 基本原理
    mac php7 连接数据库遇到的问题
    express ,koa1, koa2学习笔记
    mac mysql的安装
    webpack 给css添加前缀
    利用git将本地的代码同步到github上
    vuex 学习总结及demo
  • 原文地址:https://www.cnblogs.com/ImaY/p/7127013.html
Copyright © 2020-2023  润新知