2016-04-08 88 views
0

我有兩個下拉字段,其中第二個依賴於第一個選擇。角度下降與依賴關係的第一個下拉

第一次下拉式數據來自一個數據集。它列出了與帳戶ng-repeat="option in acctList":1,4和7相關聯的號碼。 我想要轉到其他數據集,找到匹配的帳戶號碼,然後顯示與該帳戶相關的客戶。 (賬戶1有客戶1-2,賬戶4有客戶3-4,賬戶7有客戶5-7)。當第一個選項沒有被選中時,第二個下拉菜單應該不顯示任何內容(空白)。

這是我plunker與兩個下拉菜單:http://plnkr.co/edit/jDitkge1rx6GJzkdElJO?p=preview

這裏是我的控制器:

angular.module("app", []) 
    .controller("MainCtrl", function($scope) { 
    $scope.test = "This is a test"; 
    $scope.defnum = 1; 
    $scope.acct_info = [ 
     { 
     "Req": "MUST", 
     "DefCom": "1" 
     }, 
     { 
     "Req": "NoMUST", 
     "DefCom": "5" 
     }, 
     { 
     "Req": "MUST", 
     "DefCom": "4" 
     }, 
     { 
     "Req": "MUST", 
     "DefCom": "7" 
     } 
    ]; 

    $scope.cust_info = [ 
     { 
     "Customer": "1", 
     "Com": "1" 
     }, 
     { 
     "Customer": "2", 
     "Com": "1" 
     }, 
     { 
     "Customer": "3", 
     "Com": "4" 
     }, 
     { 
     "Customer": "4", 
     "DefCom": "4" 
     }, 
     { 
     "Customer": "5", 
     "DefCom": "7" 
     }, 
     { 
     "Customer": "6", 
     "DefCom": "7" 
     }, 
     { 
     "Customer": "7", 
     "DefCom": "7" 
     } 
    ]; 
    }); 

我加ng-repeat="option in cust_info | filter:{ Com : filter.DefCom }"嘗試篩選基於該第二SO回答:Filter ng-options from ng-options selection 但它不工作。 我試過使用ng-change,但我認爲應該有一個更簡單的方法,如filtertrack by。我想知道如果我應該從ng-repeat改爲ng-option。任何人都可以通過簡單的方式瞭解這一點?

回答

0

在你的第二個<option>標籤使用ng-if,可能是,不管你想要

<select> 
    <option ng-selected="defnum == option.DefCom" ng-repeat="option in acct_info | filter:{Req:'MUST'}:true"> 
     {{ option.DefCom }} 
    </option> 

    </select> 
    <select> 
    <option ng-if="option.DefCom" ng-selected="" ng-repeat="option in cust_info"> 
     {{ option.Customer}} 
    </option> 

    </select> 
+0

這並不在本例中工作。當您在第一個下拉列表中選擇「1」時,它應該只顯示選項1和2.當您選擇「2」時,它應該顯示選項3和4等等。 – jenryb