【发布时间】:2014-04-10 16:15:47
【问题描述】:
我从一系列来源开始
$scope.sources = [
{
"type": "register",
"name": "Register 1",
"balance": 100
},
{
"type": "register",
"name": "Register 2",
"balance": 100
},
{
"type": "register",
"name": "Register 3",
"balance": 200
},
{
"type": "office",
"name": "Change Drawer",
"balance": 200
},
{
"type": "office",
"name": "Safe",
"balance": 500
}
];
我正在成功加载选项
<div class="form-group">
<label>Transfer <strong>{{amount(count, start, selectedItem.balance) | currency}}</strong> To:</label>
<select id="transferTo" class="form-control" ng-model="form.to" ng-options="item.name for item in sources | filter:{type:'office'}">
<option value="">-- Select a Source --</option>
</select>
</div>
我尝试使用$timeout 函数在它工作后选择它,但它没有将正确的值传回我的函数
$timeout(function () {
$('#transferTo').val('1');
}, 200);
如何将“安全”设置为表单加载时选择的默认选项?
【问题讨论】:
-
您希望将
form.to设置为什么值?为了唯一标识每个选项。 -
您需要将
form.to范围属性设置为选定的值。 -
我真的需要整个对象回来。我确实有
item.balance as item.name for item in sources | filter:{type:'office'},它把余额传回来了,但我需要整个对象来更新范围 -
然后,您需要将
form.to设置为“安全”数组对象。因为它将使用对象引用来比较选定的值。您还可以查看track by子句中的ng-options。 -
@DavinTryon 关于使用
track by的好点子,因为它将允许您从本质上指定一个字段来为所选项目进行匹配
标签: angularjs ng-options