0

I have make route in my app.js, well I need receive two parameters in upsr URL.

.state('upsr', {
   url: '/upsr/:QuestionIdIndex/:LangId',
   templateUrl: 'modules/study/question/question-details.html',
   controller: 'upsrCtrl'
})

How I can use $state.go without sending any parameters like below?

$state.go('upsr');

Instead of:

$state.go('upsr',{
    QuestionIdIndex : 0,
    LangId:0
});

Note: If there were no any parameters, then it will set as default value. For example

QuestionIdIndex = 0; 
LangId = 0;

My problems was if I just use $state.go('upsr'); then it would be error.

Any ideas how to solve this?

2
  • $state.go('upsr', { 'QuestionIdIndex ':'0', 'LangId ':0' }); Commented Oct 27, 2016 at 1:46
  • I knew that..I just want to use $state.go('upsr') for default instead of ` $state.go('upsr', { 'QuestionIdIndex ':'0', 'LangId ':0' }); ` Commented Oct 27, 2016 at 1:52

2 Answers 2

2

You can achieve this by using the params option when defining your state.

Note: optional parameters requires [email protected] so then it will match with $urlRouterProvider.otherwise('/upsr');

.state('upsr', {
   url: '/upsr/{QuestionIdIndex}/{LangId}',
   params: {
      QuestionIdIndex: { value: 0 },
      LangId: { value: 0 }
   },
   templateUrl: 'modules/study/question/question-details.html',
   controller: 'upsrCtrl'
})
Sign up to request clarification or add additional context in comments.

5 Comments

Also can be implement in $urlRouterProvider.otherwise('/upsr')?
@Imran maybe, which version you are using?
How do I check?
you can see on the header of angular-ui-router.js
@Imran newer versions of ui-router implements optional parameters, I've updated my answer
0

You need to make them as optional parameters,

state('upsr', {
   url: '/upsr/:QuestionIdIndex?/:LangId?',
   templateUrl: 'modules/study/question/question-details.html',
   controller: 'upsrCtrl'
})

4 Comments

Also can be implement in $urlRouterProvider.otherwise('/upsr')?
@Imran yes you can
How to set default value for QuestionIdIndex and LangId?

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.