簡體   English   中英

如何使用ng-options選擇特定選項

[英]How to select specific option using ng-options

我從一系列資源開始

$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);

加載表單時,如何將“安全”設置為默認選項?

您將需要在范圍內設置一個值,將ng-model設置為等於:

$scope.form.to = $scope.sources[4];

如果列表(源)是動態的,則可以像這樣過濾數組,這將返回一個數組(但保持不變)。

filterFilter($scope.sources, {name: 'Safe'})

小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM