0

I am trying to create a dynamic dropdown of this year and the next 3 years that will update automatically as the year changes. I am getting an unexpected token error from this attempt:

CONTROLLER:

$scope.thisYear = new Date().getFullYear();
$scope.year_span = [
   {'id'=>$scope.thisYear, 'year'=>$scope.thisYear},
   {'id'=>$scope.thisYear+1, 'year'=>$scope.thisYear+1},
   {'id'=>$scope.thisYear+2, 'year'=>$scope.thisYear+2},
   {'id'=>$scope.thisYear+3, 'year'=>$scope.thisYear+3}
];

HTML:

<div class="form-group col-md-4">
    <label>Crop Year:</label>
    <select name="crop_year" ng-model="frmData.loan.crop_year" class="form-control" ng-options="ys.id as ys.year for ys in year_span">
        <option value="">Please select ...</option>
    </select>
</div>

2 Answers 2

1

Maybe it should be

$scope.year_span = [
{'id': $scope.thisYear, 'year':$scope.thisYear},
{'id': $scope.thisYear+1, 'year':$scope.thisYear+1},
{'id': $scope.thisYear+2, 'year':$scope.thisYear+2},
{'id': $scope.thisYear+3, 'year':$scope.thisYear+3}
];

as the correct syntax for object is {key:value}

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

Comments

1
$scope.year_span = [
       {'id'=>$scope.thisYear, 'year'=>$scope.thisYear},
       {'id'=>$scope.thisYear+1, 'year'=>$scope.thisYear+1},
       {'id'=>$scope.thisYear+2, 'year'=>$scope.thisYear+2},
       {'id'=>$scope.thisYear+3, 'year'=>$scope.thisYear+3}
    ];

In the above code, you are creating javascript objects inside an array. In javascript, objects are key value pairs and the correct syntax to do it is

{ key: value}

In your example, you have written

{ key => value }

This is the array syntax in PHP, so I think you have mixed the syntax

If you just change your declaration as below, things will work.

$scope.year_span = [
{'id': $scope.thisYear, 'year':$scope.thisYear},
{'id': $scope.thisYear+1, 'year':$scope.thisYear+1},
{'id': $scope.thisYear+2, 'year':$scope.thisYear+2},
{'id': $scope.thisYear+3, 'year':$scope.thisYear+3}
];

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.