sercan
sercan

Reputation: 483

how to remove route from $routeProvider

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

Answers (1)

gkalpak
gkalpak

Reputation: 48211

So, in order to remove the route you need to:

  1. have access to $route.routes.
  2. know the path you want to remove.
  3. have access to $templateCache.
  4. find out the 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

Related Questions