0

I'm using AngularJD ui-router to capture my URL and send two parameters from that URL to my controller. Here are my requirements:

  • The first parameter is a mandatory integer (id)
  • The second parameter is an optional string (slug)
  • The two parameters are separated by a /
  • I want it to ignore any single trailing /

Currently, I'm doing this:

$stateProvider.state('mystate',
  {
    url: '/mystate/{id:int}{slug:(?:/[^/]+)?}',
    params: {
      slug: {value: null}
    },
    templateUrl: 'partials/mystate.html',
    controller: function($stateParams) {
      console.log('$stateParams.id=',$stateParams.id, ' $stateParams.slug=',$stateParams.slug);
    }
  }

For URL: mystate/1 This correctly captures:

$stateParams.id=1, $stateParams.slug=null

For URL: mystate/1/myslug This captures the wrong value for slug:

$stateParams.id=1, $stateParams.slug=/asdad

For URL: mystate/1/ This doesn't capture anything.

For URL: mystate/1/myslug/ This doesn't capture anything.

How can I write this so that it captures all four of the aforementioned URLs properly but doesn't pass any of the /s to the controller?

1 Answer 1

2

For optional trailing slash, you need to set strict mode to false in a config block:

angular.module('yourModule').config(['$urlMatcherFactoryProvider', function($urlMatcherFactoryProvider) {
  $urlMatcherFactoryProvider.strictMode(false);
}]);

For having optional parameters, there's a long discussion here. At the end, they suggest using squash:

$stateProvider.state('mystate',
{
  url: '/mystate/{id:int}/:slug',
  params: {
    slug: {value: null, squash: true}
  },
  templateUrl: 'partials/mystate.html',
  controller: ['$stateParams', function($stateParams) {
    console.log('$stateParams.id=',$stateParams.id, ' $stateParams.slug=',$stateParams.slug);
  }]
}

squash has good documentation, if you set it to true, it omits the parameter from URL when it has the default value.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks!! Where do I insert the line for $urlMatcherFactoryProvider? When I inserted it right before the $stateProvider line, I got: Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to: ReferenceError: $urlMatcherFactoryProvider is not defined
You'll need to inject it first into your config function
@SaqibAli BTW you should use array notation for dependencies if you uglify/minify your code for browsers, see my controller: edit.
the $urlMatcherFactoryProvider line seems to have no effect. I put it on my app, like you instructed. The slug and the id are being captured... but ONLY if there is a trailing slash (in either case)
Did you make sure that line is executed by putting a console.log statement next to it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.