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?

Nere
  • 4,097
  • 5
  • 31
  • 71
  • $state.go('upsr', { 'QuestionIdIndex ':'0', 'LangId ':0' }); – Sajeetharan Oct 27 '16 at 01:46
  • I knew that..I just want to use `$state.go('upsr')` for default instead of ` $state.go('upsr', { 'QuestionIdIndex ':'0', 'LangId ':0' }); ` – Nere Oct 27 '16 at 01:52

2 Answers2

2

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

Note: optional parameters requires angular-ui-router@1.0.0-beta.3 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'
})
lenilsondc
  • 9,590
  • 2
  • 25
  • 40
0

You need to make them as optional parameters,

state('upsr', {
   url: '/upsr/:QuestionIdIndex?/:LangId?',
   templateUrl: 'modules/study/question/question-details.html',
   controller: 'upsrCtrl'
})
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396