Nere
Nere

Reputation: 4097

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?

Upvotes: 0

Views: 522

Answers (2)

lenilsondc
lenilsondc

Reputation: 9800

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

Upvotes: 2

Sajeetharan
Sajeetharan

Reputation: 222582

You need to make them as optional parameters,

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

Upvotes: 0

Related Questions