-2

I have conditional $stateParams on $state and i am trying to assign current $stateParams value to paramId. How can i achieve that task using ternary operator?

commonCtrl.js

 var paramId = $stateParams.processId:? $stateParams.assessmentId;

config.js

.state('app.addPrcChallenge', {
            url: '/add/prcChallenge/:processId/:assessmentId?',
            templateUrl: 'views/process/processChallenge.html',
            controller: 'ProcessChallengesCtrl',
            data: {
                authenticate: true
            },
2
  • 2
    Side note - it's called a ternary operator Commented Apr 27, 2016 at 20:52
  • Side note - you can not place : and ? together
    – Redu
    Commented Apr 27, 2016 at 20:55

2 Answers 2

2

You are close. Try this:

var paramId = $stateParams.processId ? $stateParams.processId : $stateParams.assessmentId;
3
  • why we assigning $stateParams.processId twice ?
    – hussain
    Commented Apr 27, 2016 at 21:06
  • @hussain - It's not assigning it twice.
    – RJM
    Commented Apr 27, 2016 at 21:17
  • You could use || instead. Commented Apr 28, 2016 at 0:00
1

Unfortunately javascript doesn't posses a null coalescing operator like C# does but you could achieve pretty much the same effect with this construct:

var paramId = $stateParams.processId || $stateParams.assessmentId;

Basically this means that paramId will equal to $stateParams.processId if it has some value different than undefined and equal to $stateParams.assessmentId otherwise.

2
  • Any reason for the downvote? Commented Apr 27, 2016 at 21:00
  • I can't see one. I prefer your answer. Commented Apr 28, 2016 at 0:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.