angularjs获得以前的路线路径

Gih*_*han 50 angularjs angularjs-routing

<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>
Run Code Online (Sandbox Code Playgroud)

在我的模块配置中

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

控制器

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'};
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

and*_*rsh 74

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

模板:

<a ng-click='back()'>Back</a>
Run Code Online (Sandbox Code Playgroud)

模块:

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);
    };

});
Run Code Online (Sandbox Code Playgroud)

  • @KennethLynne我试图使用`current.$$ route.originalPath`而不是`$ location.$$ path`但路由参数只在使用`$ location`时填写.如果你不使用路线参数,那么你的建议应该有效. (2认同)

Mar*_*cok 22

使用$ 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);
});
Run Code Online (Sandbox Code Playgroud)

  • 它们记录在这里:http://docs.angularjs.org/api/ngRoute.$route (2认同)
  • 感谢@ArturBodera,我看到它们现在已经记录在这里:http://docs.angularjs.org/api/ng.$location#events我更新了答案. (2认同)

Moh*_*far 16

在你的HTML中:

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

在您的主控制器上:

$scope.go_back = function() { 
  $window.history.back();
};
Run Code Online (Sandbox Code Playgroud)

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

  • @AppDevGuy` $ window`是`window`的一个角度包装器,你被鼓励使用它来代替`window`,因为这样它可以被模拟用于测试等.来自angular docs(https:// docs. angularjs.org/api/ng/service/$window):"在角度我们总是通过$ window服务引用它[到`window`],所以它可能被覆盖,删除或嘲笑以进行测试." (3认同)

小智 5

@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
      };
Run Code Online (Sandbox Code Playgroud)


Raj*_*ian 1

只是为了记录:

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

为了克服这个问题,我尝试过类似的事情。

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

请注意,在上面的路由配置中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)
    })
})
Run Code Online (Sandbox Code Playgroud)