简体   繁体   English

使用Angular从JSON过滤数据

[英]Filter data from json with Angular

I need to filter students by class and then push that to HTML pattern. 我需要按班级过滤学生,然后将其推送到HTML模式。

Here's the code: http://plnkr.co/edit/GG9EYmPlIyD4ZM0ehaq6?p=preview 这是代码: http : //plnkr.co/edit/GG9EYmPlIyD4ZM0ehaq6?p=preview

.html: .html:

<div ng-controller="ClassController">
          <table>
            <thead>
              <tr>
                <th>Name</th>
              </tr>
            </thead>
            <tbody>
              <tr ng-repeat="student in students">
                <td>{{ student.name }}</td>
              </tr>
            </tbody>
          </table>
    </div>

.js: .js:

var app;

app = angular.module('App', []);

app.controller('ClassController', [
  '$scope', '$http', function($scope, $http) {
    $scope.students = [];
    return $http.get('data.json').then(function(result) {
      return angular.forEach(result.data, function(item) {
        return $scope.students.push(item);
      });
    });
  }
]);

you can find .json file on the link 您可以在链接上找到.json文件

Here is your controller: 这是您的控制器:

var app;

app = angular.module('App', []);

app.controller('ClassController', [
  '$scope', '$http', function($scope, $http) {
    var someFilter = 'taple';
    $scope.students = [];
    return $http.get('data.json').then(function(result) {
      $scope.students = result.data.filter(function(item) {
        return item.classroom.school.name.toLowerCase().indexOf(someFilter) > -1;
      })
    });
  }
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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