我正在尝试使用ng-options
返回数组中元素的索引而不是其值:
select
指令:
<select ng-model="obj.value" ng-options="v for v in obj.values"></select>
如果数据格式如下,则工作正常:
{ "value": "value 3", "values": [ "value 1", "value 2", "value 3", "value 4"] }
问题是网络服务发送数据如下:
{ "value": 2, "values": [ "value 1", "value 2", "value 3", "value 4"] }
是否可以将指令修改为:
value
答案 0 :(得分:1)
我almost done what you need,但还有一个问题:
JS:
var demo = angular.module("demo", []);
demo.controller("DemoController", ['$scope', function($scope){
$scope.obj = {
"value": 2,
"values": [
"value 1",
"value 2",
"value 3",
"value 4"
]
};
}]);
HTML:
<body data-ng-app="demo">
<div data-ng-controller="DemoController">
<select data-ng-model="obj.value">
<option data-ng-repeat="i in obj.values"
value="{{$index}}"
data-ng-selected="$index == obj.value">
{{i}}
</option>
</select>
{{obj}}
</div>
</body>
但是在下拉列表中选择了某些内容后,obj.value
会变为字符串。 A quick fix.