how to remove route from $routeProvider

angularjs

Solution

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

- have access to `$route.routes`.

- know the path you want to remove.

- have access to `$templateCache`.

- 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.

Problem

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 ?

Original source