簡體   English   中英

AngularJS如何過濾嵌套對象

[英]AngularJS How i can filter nested object

如何在AngularJS中過濾嵌套對象? 我創建了一個函數來獲取proptypes 我想過濾type1.value = false

JSON:

scope.States = {
  "1": {
    "Prop": {
      "id": 1
    },
    "PropTypes": {
      "Type1": {
        "date": "2015-05-30T01:01:04",
        "value": false,
        "id": 1,
        "name": "defautPompier"
      },
      "Type2": {
        "date": "2015-05-30T01:01:04",
        "value": true,
        "id": 1,
        "name": "Delestage"
      }
    },
    "defaultsInstallations": ["defautPompier", "Delestage"]
  }
}

控制器:

$scope.GetDefaultByInstallation = function(title) {
    $scope.installationsStates = [];
    var res;
    $scope.result = [];
    Object.keys($scope.States).forEach(function(key) {
          var res = false;
          angular.forEach($scope.States[key].PropTypes, function(value, cle) {
                if ($scope.States[key].PropTypes[cle] == title) {
                  res = true;
                }
              }

為了過濾數據,您有兩種不同的方法:

  1. 在html內
  2. 在控制器內部的功能中

通過文檔

{{ expression [| filter_name[:parameter_value] ... ] }}

在這種情況下,由於有了“管道”,您可以在代碼中添加一個過濾器。

例如HTML:

<ul>
  <li ng-repeat="friend in person.friends | startsWithA">
    {{ friend }}
  </li>
</ul>

JS:

app.filter('startsWithA', function () {
  return function (items) {
    var filtered = [];
    for (var i = 0; i < items.length; i++) {
      var item = items[i];
      // ... your logic for filter items
      // E.G.
      if (/a/i.test(item.name.substring(0, 1))) {
        filtered.push(item);
      }
    }
    return filtered;
  };
});

您還可以過濾控制器上的數據( 必須將新的過濾列表添加到范圍中 ),然后在ng-repeat過濾列表內部添加。

暫無
暫無

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

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