我正在努力创建今年和未来3年的动态下拉列表,随着年份的变化将自动更新。我从这次尝试中收到意外的令牌错误:
控制器:
$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>
答案 0 :(得分: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}
];
因为对象的正确语法是{key:value}
答案 1 :(得分: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}
];
在上面的代码中,您将在数组中创建javascript对象。在javascript中,对象是键值对,正确的语法是
{ key: value}
在你的例子中,你写了
{ key => value }
这是PHP中的数组语法,所以我认为你混合了语法
如果您只是更改下面的声明,那么事情就会奏效。
$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}
];