3

As told by Can angularjs routes have optional parameter values? and AngularJS: Routing with URL having optional parameters question mark ? with parameter name should make it optional. Its not helping me.

var app = angular.module('app', ['ui.router','ngRoute']);

app.config(function ($urlRouterProvider, $stateProvider) {
    $urlRouterProvider.otherwise('/a');
    $stateProvider.state('a', {
        url: '/a',
        templateUrl: 'views/a.html'
    }).state('b', {
        url: '/b/:code?/:d?',
        templateUrl : 'views/b.html'
    })
});

This url http://localhost:xx/kk/#/b/1/2 works fine for me. But http://localhost:xx/kk/#/b (without any parameter) and http://localhost:xx/kk/#/b/1 not working for me...

You could see I am using $stateProvider with ui-router. I do not want to switch to $urlRouteProvider

1 Answer 1

6

There is a working plunker

What we need is a setting called params : {} and it could handle optional params:

 params: {
    code: {squash: true, value: null},
 }

So the state defintion would be

.state('b', {
    url: '/b/:code/:d',
    templateUrl : 'views/b.html',
    params: {
      code: {squash: true, value: null},
      d   : {squash: true, value: null},
    }
})

And these will work:

<a href="#/b">
<a href="#/b/code1">
<a href="#/b/code22">
<a href="#/b/code333/d4">

Check these for more details and examples:

And some more magic here:

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

6 Comments

Sorry I edited my question a bit. If you don't mind please edit the answer, otherwise i will do it after testing and getting succeeded with it
I created a plunker for you plnkr.co/edit/MXb3Eklfrw8fWRuPPk1u?p=preview, extended the answer with all the details - plus other examples in the links... hope that will help ;)
Perfect. Even working with localhost:xx/yy/#/state1/1/2/substate1/2/3... Do you think that angular could allow to choose the parameter like ttp://localhost:96/angle/#/b/d1 = > here i want to leave the code and give value of d
Well, yes, that is covered in my links... but seems you want more and more ;) and do not want to accept - while I did answered your question, right? Well if you have new question, ask new question. Would you agree? BTW here you can find another approach I do like stackoverflow.com/a/30230421/1679310 and here is the way how to play with restrictions stackoverflow.com/a/31837507/1679310
Sorry i had clicked accepted, immediately after your last edit (1 hour ago) unfortunately my connection wasn't working that time, but i noticed later (just before getting your last comment :). Yes i should ask another question, what i ultimately want to achieve is unique url for every state (after each event on ui) of application, so we could provide the testing team with those all links and tester could could have no other scenario
|

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.