2

我正在尝试将对象数组绑定到选择下拉列表,但我无法弄清楚如何使这项工作,有人可以建议如何使这项工作?

HTML:

<div ng-app ng-controller="DisplayCtrl">
<select ng-model="eventName" ng-options="name.event for name in eventNames">
    <option value="">Select Event</option>
</select>
<p>Currently selected: {{eventName.description}} </p></div>

AngularJS:

function DisplayCtrl($scope) {
$scope.eventNames = [{
    event: "Function A",
    description: "Gaming"
}, {
    event: "Function B",
    description: "Basketball"
}, {
    event: "Function C",
    description: "Football"
}, {
    event: "Function D",
    description: "Dancing"
}];
$scope.eventName = $scope.eventNames[1].event;}

http://jsfiddle.net/ztABS/

4

1 回答 1

4

使用对象数组,角度将真实对象存储在模型变量中。所以你的预选应该做同样的事情,因此:

$scope.eventName = $scope.eventNames[1].event;

应该:

$scope.eventName = $scope.eventNames[1];

看:

http://jsbin.com/uyuJuDO/1/

于 2013-08-28T08:48:29.527 回答