0

in my projetct i have a list of players, i called them atletas, in the view i do something like this:

 <div class="form-group" ng-repeat="atleta in atletas">
     <input type="checkbox" ng-model="selected[equipaData.atletas]" name="vehicle" value="Bike">{{atleta.nome}}<br>
     <input type="hidden" name="escondido" value="selected[atleta._id]"><br>
 </div>

so evertyme when i click the submit that is in the form above, did not mentioned here, i call a function on the controller, and when i do that i want to pass just all the atletas that are checked, the entire object, how can i do that?

1 Answers1

0

Have a selected attribute on each player and use that as the ng-model, then use Array.filter to only get the selected players.

angular.module('app', [])
.controller('ctrl', function($scope) {
  $scope.people = [
    {name: "Name 1", selected: false},
    {name: "Name 2", selected: false},
    {name: "Name 3", selected: false},
    {name: "Name 4", selected: false},
    {name: "Name 5", selected: false}
  ]
  $scope.sendSelected = function() {
    $scope.selectedPeople = $scope.people.filter(function(person) { return person.selected })
  }
})
<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@1.6.0" data-semver="1.6.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="app" ng-controller="ctrl">
    <div ng-repeat="person in people">
      <input type="checkbox" ng-model="person.selected">{{person.name}}
    </div>
    <button ng-click="sendSelected()">Select stuff</button>
    <div ng-repeat="person in selectedPeople">
      {{person.name}}
    </div>
  </body>

</html>
Fissio
  • 3,748
  • 16
  • 31