简体   繁体   English

AngularJS ng-repeat ID过滤器

[英]Angularjs ng-repeat filter by id

hi i have 2 tables in mysql, i call the data with php(mysqli), so i put this data in javascript var with $scope.student= <?php echo json_encode( $student) ?>; 嗨,我在mysql中有2个表,我用php(mysqli)调用数据,所以我用$scope.student= <?php echo json_encode( $student) ?>;将这些数据放入javascript var中$scope.student= <?php echo json_encode( $student) ?>; and $scope.ratings= <?php echo json_encode( $ratings) ?>; $scope.ratings= <?php echo json_encode( $ratings) ?>; 2 tables have a key id(ID_studen) 2个表具有键ID(ID_studen)

The first table have only a personal data, and the second have datails, so i need filter and show all details by ratings here code: 第一个表只有一个个人数据,第二个表有数据,因此我需要过滤并通过此处的评分代码显示所有详细信息:

<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>

check jsfiddle.net 检查jsfiddle.net

You can use $filter please see demo below 您可以使用$ filter,请参见下面的演示

 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' }]; } 
 <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> 

<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>

Here is the actual way to do inline filtering on a property: 这是对属性进行内联过滤的实际方法:

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

However note that is is more performant to pre-filter your result list, so in your controller you would put 但是请注意,预过滤结果列表的性能更高,因此在控制器中,您应该将

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

So your html would only be: 因此,您的html只会是:

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

Finally, I'm not very comfortable with php but it sounds like a weird way to populate your model. 最后,我对php不太满意,但这听起来像是一种奇怪的方式来填充模型。 You should do it in the actual javascript file and not within your HTML. 您应该在实际的javascript文件中而不是HTML中进行操作。

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

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