Angular - 通过 select 字段的 ng-options 绑定以下数据
Angular - Binding the following data via ng-options for a select field
给定以下代码:
http://jsfiddle.net/KN9xx/1102/
假设我收到一个 ajax 调用,其中包含我传递给范围变量的以下数据:
$scope.people_model = {
"people":[
{
"id":"1",
"name":"Jon"
},
{
"id":"2",
"name":"Adam"
}
]
};
我如何使用 select 框通过 ng-options 迭代 'people'?
<select
ng-options="p.name for name in people_model"
ng-model="people_model">
</select>
将您的 select 更改为 ,
<select ng-model="currentSelected" ng-options="selection.id as selection.name for selection in people_model.people"></select>
您需要访问对象people_model
中的数组people
DEMO
var app = angular.module('myapp', []);
app.controller("FirstCtrl", ["$scope",
function($scope) {
$scope.currentSelected = "1";
$scope.people_model = {
"people": [{
"id": "1",
"name": "Jon"
}, {
"id": "2",
"name": "Adam"
}]
};
}
]);
<!DOCTYPE html>
<html ng-app="myapp">
<head>
<title>To Do List</title>
<link href="skeleton.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="MainViewController.js"></script>
</head>
<body ng-controller="FirstCtrl">
<select ng-model="currentSelected" ng-options="selection.id as selection.name for selection in people_model.people"></select>
</body>
</html>
给定以下代码:
http://jsfiddle.net/KN9xx/1102/
假设我收到一个 ajax 调用,其中包含我传递给范围变量的以下数据:
$scope.people_model = {
"people":[
{
"id":"1",
"name":"Jon"
},
{
"id":"2",
"name":"Adam"
}
]
};
我如何使用 select 框通过 ng-options 迭代 'people'?
<select
ng-options="p.name for name in people_model"
ng-model="people_model">
</select>
将您的 select 更改为 ,
<select ng-model="currentSelected" ng-options="selection.id as selection.name for selection in people_model.people"></select>
您需要访问对象people_model
people
DEMO
var app = angular.module('myapp', []);
app.controller("FirstCtrl", ["$scope",
function($scope) {
$scope.currentSelected = "1";
$scope.people_model = {
"people": [{
"id": "1",
"name": "Jon"
}, {
"id": "2",
"name": "Adam"
}]
};
}
]);
<!DOCTYPE html>
<html ng-app="myapp">
<head>
<title>To Do List</title>
<link href="skeleton.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="MainViewController.js"></script>
</head>
<body ng-controller="FirstCtrl">
<select ng-model="currentSelected" ng-options="selection.id as selection.name for selection in people_model.people"></select>
</body>
</html>