<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>
In my module config
$routeProvider.
when('/', {
controller:HomeCtrl,
templateUrl:'home.html'
}).
when('/menu', {
controller:MenuCtrl,
templateUrl:'menu.html'
}).
when('/items', {
controller:ItemsCtrl,
templateUrl:'items.html'
}).
otherwise({
redirectto:'/'
});
Controllers
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'};
}
As you can see in my controllers I have hard coded the back button url and text (Actually I don't need the text as using an image). In this way I found back button navigate incorrectly in some cases. I cannot use history.back()
coz my back button changes to a menu link in home view.
So my question is how do I get the previous route path in controllers or is better way to achieve this ?
I have created a Plunker demonstration of my problem. Please check that.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…