4
I’m trying to use a Array
in a input[type=checkbox]
with Angular 1, but was unsuccessful.
angular.module('app', [])
.controller("Ctrl", function ($scope){
$scope.campos = [{nome: "Brasil"}, {nome: "Argentina"}, {nome: "Paraguai"}];
$scope.campos_selecionados = [];
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script>
<div ng-app="app" ng-controller="Ctrl">
<label ng-repeat="campo in campos">
<input type="checkbox"
ng-true-value="{{ campo }}"
ng-false-value="undefined"
ng-model="campos_selecionados[$index]">
{{ campo.nome }}
</label>
<pre>{{ campos_selecionados | json }}</pre>
</div>
In the example above, the Array
, even when the checkbox is not selected, it is marked as null
.
How can I make my checkbox generate a Array
with the items that were selected?