AngularJs indexOf of Object

1

Every time I select a bundle, I add in an array, example:

Items are some data that I get, I just passed a few to test

var items = [755, 20, "E", 274] 

$scope.selectBundle = function(){

    var info = {
        cod: items[3],
        bundle: items[0],
        espessura: items[1],
        classificacao: items[2]
    };
    // Passa as informações para o cart
    $rootScope.selectedBundle.push(info);

    // Fecha o modal
    $modalInstance.close();
}

Whenever I click to open a new modal, "items" change values and adds to the scope of selected.

What I want and am not getting is if in the next modal I'm going to open, check if the bundle information already exists in the array to leave the select button disabled

Example: link I did similar to my code to show as an example.

Some parts of my JS file are like this at the moment:

app.run(function($rootScope, $http){

    $rootScope.cod_mercad = null;

    $rootScope.selectedBundle = [];
});

app.controller('mainController', function($scope, $rootScope, $http, $location, $modal){

    $scope.open = function (bundle, espessura, classificacao, codigo) {

        $scope.items = [bundle, espessura, classificacao, codigo];

        // console.log($scope.items);
        var modalInstance = $modal.open({
            templateUrl: 'rotas/modal', // Endereço da view modal
            windowClass: 'full',
            size: 'lg',
            controller: 'modalController',
            resolve: {
                items: function () {
                    return $scope.items;
                }
            }
        });
    }
});

// Controller que define todo o modal
app.controller('modalController', function($scope, $http,$modalInstance, $rootScope, items){

    // Busca as informações do bundle passando os dados
    $http.post('/ajax/bundle', {'items':items}).success(function(data){

        $scope.dados = data['dados'];
        $scope.nchapas = data['chapas'];
        $scope.imagens = data['imagens'];

        // Define o tamanho da div que mostra as imagens secundarias
        // (Total de imagens + imagem principal) * tamanho de cada bloco somando com margin + padding do conteudo
        var width = $scope.imagens.length * 215 + 30;
        $('#imagem-secundaria').css('width',width);
    });

    // Seleção de bundle
    $scope.selectBundle = function() {

        var info = {
            cod: items[3],
            bundle: items[0],
            espessura: items[1],
            classificacao: items[2]
        };
        // Passa as informações para o cart
        $rootScope.selectedBundle.push(info);

        // Fecha o modal
        $modalInstance.close();
    }
});
    
asked by anonymous 24.11.2014 / 20:26

1 answer

1

I solved the problem in such a simple way.

// Percorre todos os itens selecionados
angular.forEach($rootScope.selectedBundle,function(value, key){
    // Informa ao botão quais bundles são verdadeiros
    $rootScope.selectBundleButton[value] = true;
});

And in the modal I leave the following

<a href="#" ng-show="!selectBundleButton[items]" ng-click="selectBundle()"> Selecionar bundle </a>
    
26.11.2014 / 17:04