-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
            },
Jojo
  • 2,720
  • 1
  • 17
  • 24
hussain
  • 6,587
  • 18
  • 79
  • 152

2 Answers2

2

You are close. Try this:

var paramId = $stateParams.processId ? $stateParams.processId : $stateParams.assessmentId;
Hacknightly
  • 5,109
  • 1
  • 26
  • 27
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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928