[英]How to select specific option using ng-options
I am starting with an array of sources 我从一系列资源开始
$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
}
];
I'm successfully loading the options 我已成功加载选项
<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>
I've tried using a $timeout
function to select it after it works, but it doesn't pass back the correct value to my function 我尝试过使用
$timeout
函数在工作后选择它,但是它没有将正确的值传递回我的函数
$timeout(function () {
$('#transferTo').val('1');
}, 200);
How would I set the "Safe" as the default option selected when the form loads? 加载表单时,如何将“安全”设置为默认选项?
You will need to set a value on your scope that you're setting ng-model equal to: 您将需要在范围内设置一个值,将ng-model设置为等于:
$scope.form.to = $scope.sources[4];
If your list (sources) is dynamic you can filter the array like this, which will return an array (but leave your array untouched). 如果列表(源)是动态的,则可以像这样过滤数组,这将返回一个数组(但保持不变)。
filterFilter($scope.sources, {name: 'Safe'})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.