设置选择索引
Set select index
所以我试图根据一些对象内部的值设置一个选择索引。当我循环浏览对象时,在控制台中,我可以看到colorID正在更新和正确,只是选择索引从未从上次选择的选项更改。我错过了什么?提前谢谢。
HTML:
<select
ng-model="colorModel"
ng-change="updateColor(colorModel.id)"
ng-options="i.label for i in colorOptions">
</select>
控制器:
$scope.colorOptions = [
{value: 'red', label: 'Red', id: 0},
{value: 'green', label: 'Green', id: 1},
{value: 'blue', label: 'Blue', id: 2},
{value: 'yellow', label: 'Yellow', id: 3}
];
//setting it here works just fine
$scope.colorModel = $scope.colorOptions[2];
$scope.nextOption = function(){
$scope.groupIndex++;
$scope.currentOption = userOptions[$scope.groupIndex];
$scope.colorModel = $scope.colorOptions[$scope.currentOption.colorID];
//I event tried this with no luck
$scope['colorModel'] = $scope.currentOption.colorID;
}
您似乎希望在$scope.colorModel
中显示的是Array $scope.colorOptions
的颜色对象,但您希望在select
中显示的却是label
,对吗?那么,ng-options
的正确语法是:
<select
ng-model="colorModel"
ng-change="updateColor(colorModel.id)"
ng-options="i as i.label for i in colorOptions">
</select>
在控制器中这样做:
app.controller("yourController", function($scope, $filter){
$scope.colorOptions = [
{value: 'red', label: 'Red', id: 0},
{value: 'green', label: 'Green', id: 1},
{value: 'blue', label: 'Blue', id: 2},
{value: 'yellow', label: 'Yellow', id: 3}
];
//setting it here works just fine
$scope.colorModel = $scope.colorOptions[2];
$scope.nextOption = function(){
$scope.groupIndex++;
$scope.currentOption = userOptions[$scope.groupIndex];
$scope.colorModel = $filter('filter')($scope.colorOptions, {id:$scope.currentOption.colorID})[0];
}
});
注意,您需要在controller
中注入$filter
服务,如下所示:app.controller("yourController", function($scope, $filter){...
工作示例
相关文章:
- 按照选项卡索引的顺序循环一个jQuery选择
- 基于类似索引的queryString的Id选择器
- 哪个更快?按 ID 选择,或按索引选择
- D3选择html——传递函数时,索引从1开始,而不是从0开始
- 我想将数组的索引附加到
中,其中选择选项是数组的索引 - jQuery:如何获取选择数组中元素的索引
- 如何使用原始选择选项的值更新引导选择的数据原始索引
- 选择的索引未设置,除非显示警报
- 下拉列表中的选定索引在未选择任何值时引发错误
- 在索引更改中选择的下拉列表无效!!由于引导
- <选择>其中选择等于源数组的索引
- 获取选择组中选择选项的索引
- 将页面元素索引到 JSON 对象?或者jQuery选择器它每次
- 如何在每次点击时更改日期时间选择器的 Z 索引
- 使用 jQuery,查找所有选择并将索引值追加为选项
- 通过不带 jquery 的索引选择一个元素
- 如何按索引选择表单元格
- jQuery通过索引选择多个表列
- 按元素名称中的索引选择元素
- Jquery按它自己的索引选择节