0

I'm trying this:

$scope.items = {
    current: item.number || 1
};

And i'm getting: Error: item.number is undefined

How to set the current to 1 when item.numberis undefined?

1
  • I believe number should be current in the object. If so, use $scope.items = { current: this.current || 1 }; Commented Aug 12, 2015 at 11:02

3 Answers 3

4

This errors because item itself is undefined. Try:

current: item && item.number ? item.number : 1

If it's possible for item.number to be 0, change this to:

current: item && item.number !== undefined ? item.number : 1

0 is falsey so would be overwritten by 1 in the first case.

Sign up to request clarification or add additional context in comments.

1 Comment

Some parens here would be nice, would make this easier to read
1

Try this:

$scope.items = {
  current: (item && typeof item.number != "undefined") ? item.number : 1
};

Comments

0

Just use a simple if-else:

$scope.items = {}
if(item.number == undefined) {
    $scope.items.current = 1;
}
else {
    $scope.items.current = item.number;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.