AngularJS过滤嵌入属性

时间:2016-10-12 12:15:38

标签: angularjs filtering

我有一个联系人对象,在这个联系人对象中包含了帐户对象:

  

vm.contacts.accounts

并且每个帐户都有一个accountNumber。现在我想通过帐号来过滤联系人。 我试过这样的话:

<input type="text" class="form-control" placeholder="Rechnungsnummer eingeben" data-ng-model="searchedAccountNumber" /> 
    <br />
    <div data-ng-repeat="contact in vm.contacts | filter:  { accounts: [{ accountNumber: searchedAccountNumber }] }"

但它不起作用。有没有人知道如何进行过滤?

1 个答案:

答案 0 :(得分:1)

您可以使用过滤功能:

//Controller
function hasSearchedAccountNumber(contact) {
  var accounts = contact.accounts;
  var accountHasAccountNumber = false;
  for (var i = 0; i < accounts.length; i++) {
    //For an exact match, could do a partial match as well)
    if (accounts[i].accountNumber === $scope.searchedAccountNumber) {
      accountHasAccountNumber = true;
      break; //If one account has the number, it's not needed to look for other accounts 
    }
  }
  return accountHasAccountNumber;
}

在你的HTML中:

<div data-ng-repeat="contact in vm.contacts | filter: hasSearchedAccountNumber(contact);"></div>