通过id进行Angularjs ng-repeat过滤

时间:2015-01-14 22:06:10

标签: javascript jquery angularjs

我在mysql中有2个表,我用php(mysqli)调用数据,所以我把这些数据放在javascript var中$scope.student= <?php echo json_encode( $student) ?>;$scope.ratings= <?php echo json_encode( $ratings) ?>; 2个表有一个密钥id(ID_studen)

第一个表只有个人数据,第二个表有数据,所以我需要过滤并按照评级显示所有细节代码:

<div ng-app="" ng-controller="Ctrl">
  <ul ng-repeat="student in students">
   <li ng-repeat="rating in ratings (where student.tagid =ratings.tagid">  {{rating.note}}</li>

  </ul>
</div>

检查jsfiddle.net

3 个答案:

答案 0 :(得分:2)

您可以使用$ filter,请参阅下面的演示

&#13;
&#13;
app = angular.module("app", []);

app.controller("Ctrl", Ctrl);

function Ctrl($scope) {

  $scope.students = [{
    firstname: 'Buster',
    lastname: 'Bluth',
    tagid: '4134'
  }, {
    firstname: 'John',
    lastname: 'McClane',
    tagid: '9845'
  }, {
    firstname: 'Mister',
    lastname: 'Spock',
    tagid: '0905'
  }];

  $scope.ratings = [{
    matter: 'Mathematics',
    note: '12',
    tagid: '4134'
  }, {
    matter: 'Biology',
    note: '13',
    tagid: '9845'
  }, {
    matter: 'Lenguage:',
    note: '14',
    tagid: '0905'
  }];
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Ctrl">
  <ul ng-repeat="student in students">
    <li ng-repeat="rating in ratings | filter : {tagid: student.tagid}">{{student.firstname}} {{student.lastname}} | {{rating.matter}} {{rating.tagid}}</li>
  </ul>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

<div ng-app="" ng-controller="Ctrl">
    <ul ng-repeat="student in students">
        <li ng-repeat="rating in ratings | { tagid = student.tagid }">{{rating.note}}</li>
    </ul>
</div>

答案 2 :(得分:0)

以下是对属性进行内联过滤的实际方法:

<li ng-repeat="rating in ratings | filter: {'tagid': student.tagid}">  {{rating.note}}</li>

但是请注意,预过滤结果列表会更有效,所以在你的控制器中你会放

$scope.filteredRatings = $scope.ratings.filter(function(r) {
    return r.tagid === $scope.student.tagid;
});

所以你的HTML只会是:

<li ng-repeat="rating in filteredRatings">  {{rating.note}}</li>

最后,我对PHP不太满意,但这听起来像是填充模型的奇怪方式。您应该在实际的javascript文件中而不是在HTML中。