简体   繁体   中英

AngularJS How i can filter nested object

How can I filter nested object in AngularJS? I created a function to get the proptypes . I want to filter 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"]
  }
}

Controller:

$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;
                }
              }

For filtering data you have two different method:

  1. Inside the html
  2. In a function inside controllers

By documentation

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

In this case thanks to the "pipe" you can add a filter in your code.

Eg 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;
  };
});

You can also filter your data on controller ( you must add to the scope a new filtered list ) and then add inside ng-repeat your filtered list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM