使用ng-model有条件地过滤ng-options

时间:2015-01-03 23:20:37

标签: javascript angularjs

我想基于ng-model过滤ng-options。

这是contactsModal数组的预览

contactsModal = [
  {
    "account" : {
        "id" : 1
    },
    "first_name" : "Test"
  },
  {
    "account" : {
        "id" : 2
    },
    "first_name" : "Test 2"
  }
];

这是我的清单

<select 
ng-model="activityAddForm.contact_id" 
ng-options="contact.id as (contact.first_name+' '+contact.last_name) for contact in contactsModal | filter:contact.account.id==activityAddForm.account_id track by contact.id">
   <option value="">Choisir...</option>
</select>

和用于条件的模型

<select 
ng-model="activityAddForm.account_id"
...

我想用同一个帐户显示联系人。像activityAddForm.account_id

我试过这条线但没有工作。

 | filter:contact.account.id==activityAddForm.account_id

任何帮助?

谢谢:)

2 个答案:

答案 0 :(得分:0)

尝试:

| filter:{ account: {id: activityAddForm.account_id } }

答案 1 :(得分:0)

尝试:

ng-options="contact.id as (contact.first_name+' '+contact.last_name) for contact in 
filteredContacts = ( contactsModal | filter:{'account':{'id':activityAddForm.account_id}}:true) track by contact.id"

安全的方式:

app.filter('filterContact', ['$filter',function($filter) {
     return function(list, value) {
       return $filter('filter')(list, function(val, key){
            return val.account.id == value;
       })
     };
 }]);


//and your ng-option

ng-options="contact.id as (contact.first_name+' '+contact.last_name) for contact in 
contactsModal | filterContact:activityAddForm.account_id track by contact.id