Imran Imran - 1 month ago 9
AngularJS Question

Default value for parameters in state.go angularjs

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?

Answer

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'
})