Send an array of objects in an ng-click?

1

I have the following doubt in this code.

<table class="table table-bordered table-hover">
        <thead>
            <tr class="bg-info table-responsive table-bordered table-striped">
                <th>Cpf</th>
                <th>Nome</th>
                <th>Marque</th>
            </tr>
        </thead>
        <tbody ng-repeat="list in listaDeAlunos" >
            <tr>
                <td>{{list.cpf}}</td>
                <td>{{list.nome}}</td>
                <td><input type="checkbox" value="{{list.cpf}}"></td>
            </tr>
        </tbody>
    </table>

I want to store in an array in the controller by ng-click all the students (all their data) selected by the checkbox, which are in this table. Is that possible?

    
asked by anonymous 30.04.2017 / 21:58

2 answers

1

You can use ng-model to directly map the status evaluation of a checkbox . Functional example below, click Run :

var myApp = angular.module("myApp", [])
    .controller("testController", function ($scope) {
    
    $scope.marcados = {};

        $scope.listaDeAlunos = [
            { cpf:'111', nome: 'Aaa' },
            { cpf:'222', nome: 'Bbb' },
            { cpf:'333', nome: 'Ccc' },
            { cpf:'444', nome: 'Ddd' },
        ];
        
    $scope.$watch('marcados', function(){
    
      angular.forEach($scope.marcados, function(v,k) {
        if (!v) delete $scope.marcados[k];
      });
    
    }, true);
        
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <body ng-app='myApp' ng-controller="testController">
    
    <table  class="table table-bordered table-hover">
        <thead>
            <tr class="bg-info table-responsive table-bordered table-striped">
                <th>Cpf</th>
                <th>Nome</th>
                <th>Marque</th>
            </tr>
        </thead>
        <tbody ng-repeat="list in listaDeAlunos" >
            <tr>
                <td>{{list.cpf}}</td>
                <td>{{list.nome}}</td>
                <td><input type="checkbox" ng-model="marcados[list.cpf]" ng-true-value="{{list}}", ng-false-value="null"></td>
            </tr>
        </tbody>
    </table>

<pre>{{marcados | json}}</pre>

    </body>
    
30.04.2017 / 23:52
0

Try this:

HTML:

ng-click="guardaObjs(list)" 

"Call the function inside the loop"

Controller:

guardaObjs(obj){ //aqui vc manipula o array (obj) }

    
30.04.2017 / 23:26