繁体   English   中英

angularjs获得以前的路线路径

[英]angularjs getting previous route path

<h1>{{header}}</h1>
<!-- This Back button has multiple option -->
<!-- In home page it will show menu -->
<!-- In other views it will show back link -->
<a ng-href="{{back.url}}">{{back.text}}</a>
<div ng-view></div>

在我的模块配置中

  $routeProvider.
  when('/', {
    controller:HomeCtrl,
    templateUrl:'home.html'
  }).
  when('/menu', {
    controller:MenuCtrl,
    templateUrl:'menu.html'
  }).
  when('/items', {
    controller:ItemsCtrl,
    templateUrl:'items.html'
  }).
  otherwise({
    redirectto:'/'
  });

控制器

function HomeCtrl($scope, $rootScope){
  $rootScope.header = "Home";
  $rootScope.back = {url:'#/menu', text:'Menu'};
}

function MenuCtrl($scope, $rootScope){
  $rootScope.header = "Menu";
  $rootScope.back = {url:'#/', text:'Back'};
}

function ItemsCtrl($scope, $rootScope){
  $rootScope.header = "Items";
  $rootScope.back = {url:'#/', text:'Back'};
}

正如你在我的控制器中看到的那样,我已经硬编码了后退按钮的URL和文本(实际上我不需要使用图像的文本)。 通过这种方式,我发现在某些情况下后退按钮导航不正确。 我无法使用history.back()因为我的后退按钮更改为主视图中的菜单链接。

所以我的问题是如何获得控制器中的先前路径路径或更好的方法来实现这一点?

我创建了一个Plunker演示我的问题。 请检查一下。

该替代方案还提供后退功能。

模板:

<a ng-click='back()'>Back</a>

模块:

myModule.run(function ($rootScope, $location) {

    var history = [];

    $rootScope.$on('$routeChangeSuccess', function() {
        history.push($location.$$path);
    });

    $rootScope.back = function () {
        var prevUrl = history.length > 1 ? history.splice(-2)[0] : "/";
        $location.path(prevUrl);
    };

});

使用$ locationChangeStart或$ locationChangeSuccess事件 ,第3个参数:

$scope.$on('$locationChangeStart',function(evt, absNewUrl, absOldUrl) {
   console.log('start', evt, absNewUrl, absOldUrl);
});
$scope.$on('$locationChangeSuccess',function(evt, absNewUrl, absOldUrl) {
   console.log('success', evt, absNewUrl, absOldUrl);
});

在你的HTML中:

<a href="javascript:void(0);" ng-click="go_back()">Go Back</a>

在您的主控制器上:

$scope.go_back = function() { 
  $window.history.back();
};

当用户单击“返回”链接时,将调用控制器功能,它将返回到上一个路径。

@andresh对我来说locationChangeSuccess工作而不是routeChangeSuccess。

//Go back to the previous stage with this back() call
var history = [];
$rootScope.$on('$locationChangeSuccess', function() {
    history.push($location.$$path);
});

$rootScope.back = function () {
          var prevUrl = history.length > 1 ? history.splice(-2)[0] : "/";
          $location.path(prevUrl);
          history = []; //Delete history array after going back
      };

这就是我目前在$rootScope存储对前一个路径的引用的方法:

run(['$rootScope', function($rootScope) {
        $rootScope.$on('$locationChangeStart', function() {
            $rootScope.previousPage = location.pathname;
        });
}]);

你需要将事件监听器耦合到Angular 1.x中的$ rootScope,但是你可能会通过不在$ rootScope上存储前一个位置的值来证明你的代码。 存储价值的更好地方是服务:

var app = angular.module('myApp', [])
.service('locationHistoryService', function(){
    return {
        previousLocation: null,

        store: function(location){
            this.previousLocation = location;
        },

        get: function(){
            return this.previousLocation;
        }
})
.run(['$rootScope', 'locationHistoryService', function($location, locationHistoryService){
    $rootScope.$on('$locationChangeSuccess', function(e, newLocation, oldLocation){
        locationHistoryService.store(oldLocation);
    });
}]);

只是为了记录:

回调参数previousRoute有一个名为$route的属性,它与$route服务非常相似。 不幸的是currentRoute参数,没有太多关于当前路线的信息。

为了克服这一点,我尝试过这样的事情。

$routeProvider.
   when('/', {
    controller:...,
    templateUrl:'...',
    routeName:"Home"
  }).
  when('/menu', {
    controller:...,
    templateUrl:'...',
    routeName:"Site Menu"
  })

请注意,在上面的路由配置中,添加了一个名为routeName的自定义属性。

app.run(function($rootScope, $route){
    //Bind the `$routeChangeSuccess` event on the rootScope, so that we dont need to 
    //bind in induvidual controllers.
    $rootScope.$on('$routeChangeSuccess', function(currentRoute, previousRoute) {
        //This will give the custom property that we have defined while configuring the routes.
        console.log($route.current.routeName)
    })
})

修改上面的代码:

$scope.$on('$locationChangeStart',function(evt, absNewUrl, absOldUrl) {
   console.log('prev path: ' + absOldUrl.$$route.originalPath);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM