2013-03-02 81 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

相關問題