2

I'm trying to constrain a route so only '/route1' or '/route2' resolve to the following rule:

$stateProvider.state("promotional", {
    url: "/{contentType}",
    templateUrl: coreConfig.path() + "/modules/content/content.tmpl.html",
    controller: "promotionalController",
    controllerAs: "contentCtrl",
    data: { requiresLogin: true }
});

for url I've tried

url: "/{contentType: (route1|route2)}"

url: "/{contentType: [(route1|route2)]}"

url: "/{contentType: [route1|route2]}"

and none of these seemed to work. Any solutions?

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
FiveTools
  • 5,970
  • 15
  • 62
  • 84
  • I'm not sure if this will solve it (may be other problems), but I'm very sure that you have to get rid of the space after `contentType:`. Also, the answer here seems to Successfully do something very similar: http://stackoverflow.com/a/25780039/624590 – DRobinson May 25 '15 at 20:59

2 Answers2

2

There is a working plunker

We can us regexp like this:

 // just routes 'route1' or 'route2'
 $stateProvider
    .state("promotional", {
        url: "/{contentType:(?:route1|route2)}",
        templateUrl: coreConfig.path() + "/modules/content/content.tmpl.html",
        controller: "promotionalController",
        controllerAs: "contentCtrl",
        data: { requiresLogin: true }
    })

    // similar but only for 'route3' or 'route4' or 'route5'
    .state("other", {
        url: "/{contentType:(?:route3|route4|route5)}",
        templateUrl: coreConfig.path() + "/modules/content/content.tmpl.html",
        controller: "promotionalController",
        controllerAs: "contentCtrl",
        data: { requiresLogin: true }
    })

Similar issues:

Check the above example here

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
1

You can use RegEx with a UrlMatcher, something like this (untested):

 var urlMatcher = $urlMatcherFactory.compile("/route[1-2]");

 $stateProvider
 .state("promotional", {
        url: urlMatcher,
        templateUrl: coreConfig.path() + "/modules/content/content.tmpl.html",
        controller: "promotionalController",
        controllerAs: "contentCtrl",
        data: { requiresLogin: true }
 });
Donal
  • 31,121
  • 10
  • 63
  • 72