1110 1110 - 6 months ago 10
Javascript Question

How to remove angular filter (show all) on specific value?

I have a radio button list and when it is selected I apply filter to list.

My first radio item have value

-1
and I want to remove any filter when that item is selected.


<tr ng-repeat="m in model.list | filter: {GoalId: selectedGoal}" >


So when selectedGoal is -1 I want to show all items (remove filter).

I tried with filter:
{GoalId: selectedGoal && GoalId != -1}
and some other variations but no success.

How can I make this?

Answer

It looks like you would be best off writing a filter function.

https://docs.angularjs.org/api/ng/filter/filter

    <tr ng-repeat="m in model.list | filter: myFilterFn" >

    $scope.myFilterFn = function(m) {
      return (m.goalId === $scope.selectedGoal) || (m.goalId === -1);
    }