我有这个路线配置:
$routeProvider
.when('/clients', {
templateUrl: 'templates/admin.clients.html'
})
.when('/clients/:client_id', {
templateUrl: 'templates/admin.clients.html',
resolve: {
myApp: function($route, $routeParams) {
$route.current.params.client_id = parseInt($route.current.params.client_id, 10);
return;
}
}
})
.when('/objections', {
templateUrl: 'templates/admin.objections.html'
})
.when('/objections/:objection_id', {
templateUrl: 'templates/admin.objections.html',
resolve: {
myApp: function($route, $routeParams) {
$route.current.params.objection_id = parseInt($route.current.params.objection_id, 10);
return;
}
}
});
如何做到这一点: 1)我从页面#/客户端开始; 2)转到#/ clients / 2; 3)转到#/问题 4)当我点击链接#/客户端时,我必须被重定向到最后查看的客户端(#/ clients / 2),而不是#/ clients。
怎么做?
答案 0 :(得分:0)
有几种方法可以满足您的需求:您可以使用共享服务(在我们的工厂中为工厂)来存储最新访问过的client_id
。
这样,当您输入/clients
路线时,您会检查client_id
的服务,并重定向到该客户的页面:
angular.module('your-app-name')
// define the routing system
.config(function($routeProvider) {
$routeProvider
.when('/clients', {
templateUrl: 'templates/admin.clients.html',
controller: 'ClientCtrl'
})
.when('/clients/:client_id', {
templateUrl: 'templates/admin.clients.html',
resolve: {
myApp: function($route, $routeParams, sharedData) {
sharedData.client_id = $route.current.params.client_id = parseInt($route.current.params.client_id, 10);
return;
}
}
});
// other routes go here
})
// define the client controller
.controller('ClientCtrl', function($location, sharedData) {
if(sharedData.client_id) {
$location.path('clients/' + sharedData.client_id);
}
// other controller logic
})
// define the sharedData factory
.factory('sharedData', function() {
// it's an empty container for now
var sharedData = {};
return sharedData;
});
虽然这不是最干净的解决方案,但我认为它将满足您的需求。
答案 1 :(得分:0)
我必须在控制器之前做一些路由操作,所以我使用这种方式:
myApp.value('last_visits', {
client: null,
objection: null,
question: null,
finalQuestion: null
})
myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/clients', {
templateUrl: 'templates/admin.clients.html',
resolve: {
myApp: function($location, last_visits) {
if ( last_visits.client!==null ) {
$location.path('/clients/'+last_visits.client);
}
}
}
}).when('/clients/:client_id', {
templateUrl: 'templates/admin.clients.html',
resolve: {
myApp: function($route, $routeParams, last_visits) {
$route.current.params.client_id = parseInt($route.current.params.client_id, 10);
last_visits.client = $route.current.params.client_id;
return;
}
}
}).when('/objections', {
templateUrl: 'templates/admin.objections.html',
resolve: {
myApp: function($location, last_visits) {
if ( last_visits.objection!==null ) {
$location.path('/objections/'+last_visits.objection);
}
}
}
}).when('/objections/:objection_id', {
templateUrl: 'templates/admin.objections.html',
resolve: {
myApp: function($route, $routeParams, last_visits) {
$route.current.params.objection_id = parseInt($route.current.params.objection_id, 10);
last_visits.objection = $route.current.params.objection_id;
return;
}
}