Opierając się na Odpowiedź Rishabha i użycie lokalizacji sergey.skipReload od this Angular Issue możesz użyć następujących opcji, aby utworzyć okno dialogowe zmiany trasy, odroczyć zmianę adresu URL na czas nieokreślony (w efekcie "anulowanie" zmiany trasy) i przepisać pasek adresu URL z powrotem na "/"bez powodowania ponownego przeładowania:
//Override normal $location with this version that allows location.skipReload().path(...)
// Be aware that url bar can now get out of sync with what's being displayed, so take care when using skipReload to avoid this.
// From https://github.com/angular/angular.js/issues/1699#issuecomment-22511464
app.factory('location', [
'$location',
'$route',
'$rootScope',
function ($location, $route, $rootScope) {
$location.skipReload = function() {
var lastRoute = $route.current;
var un = $rootScope.$on('$locationChangeSuccess', function() {
$route.current = lastRoute;
un();
});
return $location;
};
return $location;
}
]);
app
.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/home', {
controller: 'HomeCtrl',
templateUrl: 'Home/HomeView.html'
})
.when('/profile/:userId/changepwd', {
template: ' ',
controller: '',
resolve: {
data1: function($dialog, location, $q){
$dialog.messageBox(title, msg, btns)
.open()
.then(function(result){
//fires on modal close: rewrite url bar back to '/home'
location.skipReload().path('/home');
//Could also rewrite browser history here using location?
});
return $q.defer().promise; //Never resolves, so template ' ' and empty controller never actually get used.
}
}
})
.otherwise({
redirectTo: '/'
});
Wydaje się, że wycieknie to nierozwiązane obietnice, i może pojawić się lepsze rozwiązanie, ale zadziałało to dla moich celów.
Kiedy mówisz "anuluj" trasę, masz na myśli wysłanie użytkownika z powrotem tam, skąd przyszedł ('$ window.history.back()') lub przekierowanie go po zakończeniu interakcji z oknem dialogowym (' $ location.path ('/ new') ')? –