根据object.object.object.property和object.property过滤3个级别

时间:2016-08-07 15:56:15

标签: javascript angularjs filter

我试图按<script> $(document).ready(function(){ $("#myButton").click(function(){ var ls = document.createElement('link'); ls.rel="stylesheet"; ls.href="myCSSfile.css"; document.getElementsByTagName('head')[0].appendChild(ls); }); }); </script> <button id="myButton">Add / Remove</button> year._id过滤下面的对象:

major._id

我试过这样的事:

student = {
  _id: ,
  name: ,
  timetable: {
    name: ,
    _id: ,
    academic_year: {
      _id: ,
      name:
    },
    major: {
      _id: ,
      name: ,
      abbreviation_name:
    }
  }
}

和这个

<span ng-repeat="student in students |filter:{timetable: {academic_year: {_id: yearSearchIdKey}}} ></span>

但两者都不起作用 我该怎么办?

1 个答案:

答案 0 :(得分:0)

我认为你有一个array,所以filter必须是这样的:

<span ng-repeat="student in students | filter: { student: { timetable: { academic_year: { _id: yearSearchIdKey }}}}" ng-bind="student | json"></span>

工作code

(function() {
  'use strict';

  angular
    .module('app', [])
    .controller('MainCtrl', MainCtrl);

  MainCtrl.$inject = ['$scope'];

  function MainCtrl($scope) {
    $scope.students = [{
      "student": {
        "_id": "studentId1",
        "name": "student1",
        "timetable": {
          "name": "time1",
          "_id": "timeId1",
          "academic_year": {
            "_id": "academic_yearId1",
            "name": "academic_year1",
          },
          "major": {
            "_id": "majorId1",
            "name": "major1",
            "abbreviation_name": "majorAbbr1"
          }
        }
      }
    }];
  }

})();
<!doctype html>
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
</head>

<body ng-controller="MainCtrl">
  <input type="text" ng-model="yearSearchIdKey" placeholder="Search...">
  <hr>
  <pre ng-repeat="student in students | filter: { student: { timetable: { academic_year: { _id: yearSearchIdKey }}}}" ng-bind="student | json"></pre>
</body>

</html>