【问题标题】:AngularJS filtering over embedded propertyAngularJS过滤嵌入属性
【发布时间】:2017-02-21 05:48:40
【问题描述】:

我有一个联系人对象,并且在此联系人对象中包含帐户对象:

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 }] }"

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

【问题讨论】:

  • 试试filter: ( accounts: [{ accountNumber: searchedAccountNumber }] )
  • 感谢您的回复,很抱歉仍然没有显示任何内容

标签: angularjs filtering


【解决方案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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2015-11-30
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 2015-08-08
    相关资源
    最近更新 更多