Skip to content Skip to sidebar Skip to footer

Angular Ui-router $urlrouterprovider.when Handler Result Ignored By $state

I added a when() method to set/validate a route prefix, which looks something like this: $urlRouterProvider.when(/[a-z]{2}-[a-z]{2}/, [ '$match','myValidatorSrv', function($mat

Solution 1:

There is a working plunker

This would be the adjusted .when()

$urlRouterProvider.when(/[a-z]{2}-[a-z]{2}/,
  ['$match', function($match) {

    var supported = ['cs-cz', 'en-us', 'en-gb'];  
    var isSupported = supported.indexOf($match[0]) >= 0 ;
    if(isSupported){
      returnfalse;
    }
    return$match.input.replace($match[0], 'en-us');
  }
]);
$urlRouterProvider.when(/^(.(?![a-z]{2}-[a-z]{2}))/,
  ['$match', function($match) 
  {
    return$match.input.replace('/', '/en-us/');
   }
]);

That all is related to the:

$urlRouterProvider - when() for redirection

small cite:

handler as Function

If the handler is a function, it is injectable. It gets invoked if $location matches. You have the option of inject the match object as $match

The handler can return:

  • falsy to indicate that the rule didn't match after all, then $urlRouter will continue trying to find another one that matches.
  • a String, which is treated as a redirect and passed to $location.url()
  • nothing or any truthy value tells $urlRouter that the url was handled

Check it here

Post a Comment for "Angular Ui-router $urlrouterprovider.when Handler Result Ignored By $state"