【问题标题】:angularjs filter by an array return all matching resultsangularjs 通过数组过滤返回所有匹配的结果
【发布时间】:2014-12-05 17:23:37
【问题描述】:

我从$stateParams 收到一个数组,我希望用它来过滤另一个数组的内容。该数组包含一组练习的IDs。我希望显示从 $stateParams 恢复的练习 IDs

console.log 给了我一个很好的[1,2,3] ID 数组,但是我需要过滤并显示与数组中的 ID 匹配的所有结果。

这是控制器中当前用于测试的数据。

 $scope.exerciseIds = $stateParams.exerciseIds;

 $scope.singleProgrammes = [
 {exerciseId: 1, exerciseName: 'Deadlift', image: 'img/deadlift.png'},
 {exerciseId: 2, exerciseName: 'Squat', image: 'img/squat.png'},
 {exerciseId: 3, exerciseName: 'Lunge', image: 'img/lunge.png'},
 {exerciseId: 4, exerciseName: 'Snatch', image: 'img/snatch.png'},
 {exerciseId: 5, exerciseName: 'Overhead Squat', image: 'img/overhead-squat.png'},
];

这是视图中的代码:

<ion-item class="item item-thumbnail-left" ng-repeat="programme in programmes| filter:exerciseID" href="#">

编辑

【问题讨论】:

    标签: arrays angularjs filter


    【解决方案1】:

    在你看来:

    <ion-item class="item item-thumbnail-left" ng-repeat="programme in programmes| filter: filterIds" href="#">
    

    在你的控制器中:

    // {{x|filter:filterIds}} will run this function once for each item in x
    $scope.filterIds = function(item) {
        if ( $stateParams.exerciseIds.indexOf( item.excerciseID ) > -1 ) {
            return true; // the ID is in the array
        } else {
            return false; // the ID is not in the array
        }       
        // Micro version:
        // return ~$stateParams.exerciseIds.indexOf( item.excerciseID );
    }
    

    【讨论】:

      【解决方案2】:

      这样的东西应该可以工作

            <ion-item class="item item-thumbnail-left" ng-repeat="programme in programmes|
                     filter:doFilter(item)" href="#">
      
      
            $scope.doFilter = function(item) {
                return function(programme) {
            //        if programme.id in your list of ones to display 
            //             return true
            //        else 
            //            return false
                };
            };
      

      【讨论】:

      • 恐怕我需要更多帮助 - 请查看我的编辑,了解我如何获得 $stateParams 谢谢
      猜你喜欢
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 2014-08-13
      • 1970-01-01
      相关资源
      最近更新 更多