How to KEEP ONLY what was filtered in AngularJS?

0

I have the following AngularJS file:

<script>
    angular.module('meuModulo', [])

        .controller('mercadoriaCarrinho', function($scope, $http) {
            var ctrl = this;
            $scope.listademercadoria = [];
            $scope.listaDoCarrinho = [];
            $scope.listo = [];
            $scope.showPanel = true;
            $scope.total = 1;
            $scope.moeda = null;

            $scope.mercadoria0 = {
                id: 'id1',
                setor: 'alimento',
                foto: 'foto1',
                descr: 'descr1',
                de: de1,
                por: por1,
                mercadoria: '0',
                quantidade: 0,
                total: 5
            }
            $scope.listademercadoria.push($scope.mercadoria0);

           $scope.mercadoria1 = {
                id: 'id2',
                setor: 'sobremesa',
                foto: 'foto2',
                descr: 'descr2',
                de: de2,
                por: por2,
                mercadoria: '0',
                quantidade: 0,
                total: 5
            }
            $scope.listademercadoria.push($scope.mercadoria1);

            $scope.filtrarSetor = function(zSetor) {
                $scope.listaMercadoriaNova = $scope.listademercadoria.filter(
                    function(mercadoria) {
                        return mercadoria.setor != zSetor;
                    });

                $scope.listademercadoria = $scope.listaMercadoriaNova;
            }
        });
</script>

HTML code:

<div class="body" ng-controller="mercadoriaCarrinho">
    <ul class="kbtg-departamentos-menu" role="toolbar">
        <li><span>Página inicial</span></li>
        <li ng-click="filtrarSetor('alimento'); "><span>Alimentos</span></li>
        <li ng-click="filtrarSetor('sobremesa'); "><span>Sobremesas</span></li>
    </ul>


<div ng-repeat = "mercadoria in listademercadoria" class="swiper-slide">
{{mercadoria.descr}}</div>

</div>

The problem is that it excludes all filtered items, and I need to KEEP only the filtered ones.

Does anyone have any idea how to do it?

I have tried to invert the equality signs, use ".pop", $scope.listademercadoria = $scope.listademercadoria - $scope.listaMercadoriaNova; , etc ...

Note: ng-repeat and other AngularJS functions are working normally.

    
asked by anonymous 27.09.2017 / 18:59

1 answer

1

You are being asked to display all items other than the amount you received.

Change this line:

return mercadoria.setor != zSetor;

To:

return mercadoria.setor === zSetor;

    
29.09.2017 / 01:45