Reputation: 483
Is it possible to remove a route from $routeProvider ?
app.config(function($routeProvider) {$routeProvider
.when('/test', {
templateUrl:'/index.html',
controller: 'mainCtrl'
})});
How to remove the '/test' route from provider as well as templatecache ?
Upvotes: 4
Views: 2612
Reputation: 48211
So, in order to remove the route you need to:
$route.routes
.$templateCache
.templateUrl
associated with the path.(Keep in mind that $routeProvider
always registers two routes: one with a trailing /
and one without - just to cover both cases.)
function headerCtrl($location, $route, $scope, $templateCache) {
...
// For simplicity's sake we assume that:
// 1. `path` has no trailing `/`
// 2. the route associated with `path` has a `templateUrl`
// 3. everything exists, so we don't check for empty values
$scope.removeRoute = function (path) {
var route1 = $route.routes[path];
var route2 = $route.routes[path + '/'];
var tmplUrl = $route.routes[path].templateUrl;
$templateCache.remove(tmplUrl);
delete(route1);
delete(route2);
// It might also be a good idea
// to move to another, default path
$location.path('/');
};
See, also, this short demo.
Upvotes: 5