2013-03-02 34 views
45
<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> 

在我的模块配置angularjs越来越以前的路线路径

$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'}; 
} 

正如你可以在我的控制器看,我硬编码的返回按钮链接和文本(其实我不需要使用文字作为使用图像)。通过这种方式,我发现后退按钮在某些情况下导航不正确。我无法使用history.back()因为我的后退按钮变为主页视图中的菜单链接。

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

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

+0

为什么当AngularJS默认提供深层链接/后退按钮功能时,您需要手动处理返回按钮导航(例如:http://wordcharconvertor.rogtopia.com/)? – Stewie 2013-03-02 14:51:45

+0

@stewie对不起,我是angularjs的新手,我不太明白你的意思。你能否以更好的解释提供更好的例子?请注意,我的后退链接导航的行为与浏览器的后退按钮稍有不同 – Gihan 2013-03-02 15:11:28

+0

您应该按照@MarkRajcok的答案。 – Stewie 2013-03-03 01:00:47

回答

0

只是为了文档:

的回调参数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) 
    }) 
}) 
+0

我在控制台中看到日志,但我仍然没有看到如何回到历史中。我想我已经在Angular中遇到了这个问题:http://stackoverflow.com/questions/17712735/cant-stay-on-same-page-route-after-closing-modal-window-in-angular- js-on-ipad?noredirect = 1#comment25816387_17712735 – Paul 2013-07-19 04:41:58

21

使用$locationChangeStart or $locationChangeSuccess events,第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); 
}); 
+2

他们记录在这里:http://docs.angularjs.org/api/ngRoute.$route – 2013-12-08 10:16:57

+2

谢谢@ArturBodera,我看他们现在记录在这里:http:// docs.angularjs.org/api/ng.$location#events我更新了答案。 – 2013-12-09 16:17:05

+0

这节省了我的一天......当用户在浏览器中操作“返回”和“前进”按钮导致'$ routeParams'之一发生更改时,我遇到了无法重新加载控制器的问题。有了这些事件,我可以捕捉到这个导航。 – 2014-12-02 14:06:46

66

这种替换还提供了备用功能。

模板:

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

}); 
+1

顺便说一句,我不认为你需要使用$位置,因为$ routeChangeSucces回调调用两个参数:current,next。 http://docs.angularjs.org/api/ngRoute.$route – 2013-11-22 13:52:09

+2

@KennethLynne我尝试使用'current。$$ route.originalPath'而不是'$ location。$$ path',但路由参数只填充在使用'$ location'时。如果你不使用路由参数,那么你的建议应该工作。 – 2014-03-18 16:51:47

+0

它应该与本地存储一起使用以保持持久性。否则,如果页面刷新,历史将消失。 – Gihan 2016-02-12 04:33:17

0

修改上面的代码:

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

如果您使用路由参数,则该修改不起作用。 'originalPath'参数未填写 – 2014-03-18 16:49:18

+1

$ scope.on导致错误'$ scope.on不是函数。'我想你使用$ scope。$来代替。 – sunghun 2014-03-29 04:37:18

3

这是我目前如何存储在前面的路径中的参考$rootScope

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

@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 
     }; 
3

你需要夫妇在角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); 
    }); 
}]); 
+0

智能解决方案的目标是保存全球网址,否则对于单例@MarkRajcok解决方案是一个好方法 – 2016-04-29 08:05:50

14

在你的HTML:

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

在你的主控制器:

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

当用户点击在返回链接上,控制器功能被调用并且它将回到先前的路由。

+0

在我的情况下,我并不需要'''',我只是使用'window.history.back();' – 2016-06-16 03:27:53

+3

@AppDevGuy'$ window'是'window'的一个角度包装,我们鼓励您使用它来代替'window',因为这样可以模拟测试等。 从角度docs(https:// docs .angularjs.org/api/ng/service/$ window): “在angular中,我们总是通过$ window服务将它引用到'window',所以它可能被覆盖,删除或者模拟以进行测试。 – DarthVanger 2017-01-13 16:45:41

相关问题