Record in the bank only checked chekbox in the angle

0

How do I record only checkboxes that are checked. See, I have this example and when I record it is recording the 22 items.

<li>
    <label class="requerido">Profissoes</label>
        <ul>
            <li ng-repeat="item in objeto.profissoes" ng-class="{'editando':item.editando}">
                <input type="checkbox" ng-model="item.selecionado" ng-checked="item.checked"/>{{item.nome}}
            </li>
      </ul>
</li>

This code is in my view . As for Controller and etc, they are working. There are no errors, just that when recording, it records all the professions and not just the checks.

    
asked by anonymous 22.09.2016 / 21:35

1 answer

0

Well, come on.

This was the solution I found in relation to what I understood about your problem.

You have a list of professions already populated and from the selections that the user makes in the same you want to save in some type of storage.

Below is the code I made so that all selected professions were stored in a specific list.

angular.module('app', [])
.controller('testCtrl', function($scope, $http) {
    
    $scope.objeto = {
        profissoes: [
            {
                id: 1,
                nome: 'prof1',
                editando: false,
                selecionado: false
            },
            {
                id: 2,
                nome: 'prof2',
                editando: false,
                selecionado: false
            }
        ]
    };

    $scope.profSelect = new Array();

    $scope.mudaLista = function(item){
        if(item.selecionado) {
            $scope.profSelect.push(item);
        } else {
            $scope.profSelect.splice(item, 1);
        }
    };
});
<!DOCTYPE html>
<html ng-app="app">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script></head><body><divng-controller="testCtrl as sic">
      <label class="requerido">Profissoes</label>
      <ul>
         <li ng-repeat="item in objeto.profissoes" ng-class="{'editando':item.editando}">
            <input type="checkbox" ng-model="item.selecionado" ng-checked="item.selecionado" ng-change="mudaLista(item)"/>{{item.nome}}
         </li>
      </ul>
      <pre ng-bind="profSelect | json"></pre>
   </div>
</body>

As you can see, I have used a ng-change so that with each change (in this case every check ) the system adds / removes the profession from the specific list for selected items profSelect ).

From there you can store it wherever you want.

I hope I have helped:)

    
23.09.2016 / 15:40