gpt4 book ai didi

javascript - 如何绑定(bind)数组以供 Angularjs 选择?

转载 作者:行者123 更新时间:2023-11-29 18:21:16 26 4
gpt4 key购买 nike

我正在尝试将一个对象数组绑定(bind)到一个选择下拉列表,但我不知道如何进行这项工作,有人可以建议如何进行这项工作吗?

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/

最佳答案

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

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

应该是:

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

见:

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

关于javascript - 如何绑定(bind)数组以供 Angularjs 选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18483224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com