Write to bank only chekbox checked at angular

Asked

Viewed 190 times

0

How do I record only checkboxes that are checked. Check this out, I have this example and when I record 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>

That code is in mine view. As to the Controller etc., are working. There are no errors, only that when recording records all professions and not only checked.

  • Somehow, I fail to say that what goes to the bank is just the ones selected. Simply item.checked is not enough.

1 answer

0

Well, come on.

That was the solution I found regarding what I understood of your problem.

You have a list of already populated professions and from the selections that the user makes in it you want to save in some kind of Storage.

Below is the code I made for all selected professions to be 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>
  <div ng-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 used a ng-change for each change (in the case of each check in) done 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’ve helped :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.