Raghavendra
Raghavendra

Reputation: 5387

Dynamic route params in AngularJS

I am familiar with angularjs

When I want to add my routing to my application I use $routeProvider

In $routeProvider.when(), I define routes.

How to define a route If I have a directory browser like structure like:

http://myfolderbrowserapp.com/:folder1/:folder2/.../:folderN

Is it possible with angularjs like:

$routeProvider.when('/**', {
    template:'template'
})

Is this possible with angular routing? If not is there a workaround?

Please help

Upvotes: 1

Views: 843

Answers (1)

przno
przno

Reputation: 3504

From $routeProvider docs

path can contain named groups starting with a colon and ending with a star: e.g.:name*. All characters are eagerly stored in $routeParams under the given name when the route matches.

So you could define

$routeProvider.when('/:folders*', {
    template:'template'
})

And then (in controller e.g.)

$rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
    if (angular.isDefined($routeParams.folders))
        var foldersArray = $routeParams.folders.split('/');
});

However the path definition /:folders* is too generic and would probably be matched even in cases when you do not want to (don't know how your other paths look like).

Upvotes: 3

Related Questions