我有一本字典,所以在json中
Object {1: "value1", 4: "value2" }
我想传递选定的值而不是键。
现在我的代码如下:
<select ng-model="myModel.myValue" ng-options="key as item for (key, value) in items" class="form-control">
<option value="">Choose</option> </select>
答案 0 :(得分:1)
试试这个
ng-options="value as value for (key, value) in items
你是ng-options="key as item for (key, value) in items"
这里你使用密钥作为价值,这是你的答案不起作用的主要原因
您需要使用密钥对的值作为列表的值,并在视图面板中使用相同的值。
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.items = {1: "value1", 4: "value2" };
$scope.myModel = {}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<select ng-model="myModel.myValue" ng-options="value as value for (key, value) in items" "> </select>
{{myModel.myValue}}
</div>
答案 1 :(得分:0)
尝试在选项中使用ng-repeat
而不是ng-options
var app = angular.module('anApp', []);
app.controller('aCtrl', function($scope) {
$scope.items = {1: "value1", 4: "value2" };
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="anApp" ng-controller="aCtrl">
<select ng-model="myModel.myValue" class="form-control">
<option ng-repeat="(key,value) in items" value="{{value}}">
{{value}}
</option>
</select>
</div>
答案 2 :(得分:0)
使用此ng-options="value as value for (key, value) in items"
。它会为ng-model
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.items = {1: "value1", 4: "value2" };
$scope.myModel = {}
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<select ng-model="myModel.myValue" ng-options="value as value for (key, value) in items" class="form-control">
<option value="">Choose</option> </select>
{{myModel.myValue}}
</div>
&#13;
答案 3 :(得分:0)
见:
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.items = {1: "value1", 4: "value2" };
$scope.myModel = "value1";
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<select ng-model="myModel" ng-options="value for (key, value) in items" "> </select>
{{myModel.myValue}}
</div>
&#13;