How to create filter in Angularjs?

后端 未结 2 1353
无人及你
无人及你 2021-01-23 07:03

I have this collection of courses:

 [{ id: 1, courseId: 2, text: \'John\' },
  { id: 2, courseId: 2, text: \'Willi\' },
  { id: 3, courseId: 2, text: \'Inga\' },         


        
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-23 07:27

    You could create your custom filter so that can provide you the filtered values, filter should take array of element to be filter array.

    Markup

    ng-repeat="course in courses| customFilter: [{"id": 3},{"id": 2},{"id": 1}]""
    

    Filter

    app.filter('customFilter', function(){
      return function(array, filterArray){
         var ids = [];
         angular.forEach(filterArray, function(val, index) {
            ids.push(val.id);
         }
         return array.filter(function(value){
            return ids.indexOf(value.id) !== -1;
         });
      }
    })
    

提交回复
热议问题