Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to have wildcards in angular routes [duplicate]

Tags:

angularjs

Possible Duplicate:
AngularJS - Route - How to match star (*) as a path

How do I specify wildcards in my routes -

$routeProvider
      .when('/admin/*', {
        templateUrl: 'admin.html',
        controller: 'AdminCtrl'
      })

So the above should work for /admin/users and /admin/users/1 or /admin/org/3. So there could be either one or two levels of path after admin. How do I do it ?

like image 535
murtaza52 Avatar asked Jan 08 '13 13:01

murtaza52


People also ask

What does wildcard * * In a route path represent?

A Wildcard route has a path consisting of two asterisks (**). It matches every URL, the router will select this route if it can't match a route earlier in the configuration. A Wildcard Route can navigate to a custom component or can redirect to an existing route.

What does the * * path in Angular router do?

The two asterisks, ** , indicate to Angular that this routes definition is a wildcard route. For the component property, you can define any component in your application.

Which wildcard is used to define the Page Not Found route?

Using below wild card we can define page not found route D.

What is nested routing in Angular?

Nested routes are routes within other routes. In this tutorial, we will show you how to create a child route and display the child components. The Angular allows us to nest child routes under another child routes effectively creating a Tree of routes.


1 Answers

Currently AngularJS does not support regular expression in routes.

You can workaround as follows

 app.config(['$routeProvider', function($routeProvider) {
        $routeProvider
                 .when('/admin', {templateUrl: 'admin.html', controller: 'AdminCtrl'})
                 .when('/admin/:type', {templateUrl: 'admin.html', controller: 'AdminCtrl'})
                 .when('/admin/:type/:id', {templateUrl: 'admin.html', controller: 'AdminCtrl'});  
 }]);

http://plnkr.co/edit/tBumW2oEqki2sEl1hjSc?p=preview

IMO, it is good idea to have the separate controller for both admin and users, unless otherwise you have some special requirement.

like image 167
venkat Avatar answered Sep 21 '22 07:09

venkat