JSON array with checkbox AngularJS

3

I'm trying to form a JSON with what is selected by checkbox , but when I select it it inserts, but when I try to get check , it reinserts. The correct would work this way , but forming a array of JSON and not just words or numbers.

var crud = angular.module('crud',[]);


crud.controller('CadUserController',['$scope', function ($scope) {
	
$scope.dbProfiles = [{"id":1,"nome":"Adminstrador"},{"id":2,"nome":"Tabeliao"},{"id":3,"nome":"Substituto"},{"id":4,"nome":"Escrevente"},{"id":5,"nome":"Caixa"},{"id":6,"nome":"Auxiliar"}];

	  // selected fruits
	  $scope.selection = [];
	  var profiles= [];

	  // toggle selection for a given fruit by name
	  $scope.toggleSelection = function toggleSelection(profileId) {
	    var idx = $scope.selection.indexOf({"idProfile":profileId});
	    // is currently selected
	    if (idx > -1) {
	      $scope.selection.splice(idx, 1);
	    }
	    // is newly selected
	    else {
	      $scope.selection.push({"idProfile":profileId});
	    }
	  };
 }]);
<div ng-app="crud"> 
<div ng-controller="CadUserController" class="col-md-9 form-group">
   <label for="perfil">Perfil</label><br>            

   <span ng-repeat="profile in dbProfiles">
     <input type="checkbox" value="{{profile.id}}" 
            ng-checked="selection.indexOf(profile.id) > -1" 
            ng-click="toggleSelection(profile.id)"> {{profile.nome}}

   </span>
   {{selection|json}}
</div>
  </div>
<script src="resources/js/angular/angular.min.js"></script>
    
asked by anonymous 10.12.2015 / 19:36

1 answer

2

I did not understand very well, but from what I saw in a comment you wanted something more "complete" and not just get the [1,2,3] .

See if this example I put together can help: link

Updated

I think I got a better understanding of the problem, if the above example does not resolve, try this: link

In the function that is called in ng-click it passes a String together with the profile code: ng-click="toggleSelection('profile: '+profile.id)"

In this way the result will look like this:

    
11.12.2015 / 11:43