Good evening to everyone:
-
I'm working on a form with AngularJS.
-
The idea is that by marking a field, more options are opened exclusive for that field (that is, only if that option is marked).
-
And I can only mark a field of all possible.
With these premises, especially the number 3, I'm testing with radio buttons. But one thing is getting complicated: when I select a radio button, I can not find a way to make the previous one go to false (all values are true or false). That is, we still see the exclusive options for when that radio button or checkbox are marked (and its model is true). In the image you can see notebooks when they should be hidden.
I leave a capture:
If I use checkbox, it's automatic:
<label key="admin.prototexto.form.caderno" for="caderno" class="col-sm-8 col-sm-offset-1" >
<input type="checkbox" id="caderno" ng-model="ctrl.item.caderno">
{{'admin.prototexto.form.caderno' | translate}}
</label>
The model is updated automatically when you mark and unmark.
My question is, what is the correct way to do this in angularJS with radio buttons or with checkboxes so that only one can be marked (I'm working with version 1.x, which never I've used so far).
Thank you very much.
Edited 1 to add requested data
The code I use with radio button is as follows:
<label key="admin.prototexto.form.folhasSoltas" for="folhasSoltas" class="col-sm-8 col-sm-offset-1" >
<input type="checkbox" id="folhasSoltas" ng-model="ctrl.item.folhasSoltas" name="optradio" ng-value="true" >
{{'admin.prototexto.form.folhasSoltas' | translate}}
</label>
<label key="admin.prototexto.form.caderno" for="caderno" class="col-sm-8 col-sm-offset-1" >
<input type="radio" id="caderno" ng-model="ctrl.item.caderno" name="optradio" ng-value="true" >
{{'admin.prototexto.form.caderno' | translate}}
</label>
The driver:
(function () {
'use strict';
angular
.module('app')
.component('prototextoFormAdmin', {
templateUrl: 'app/admin/dominio/prototexto/prototexto-form.admin.html',
controller: Controller,
controllerAs: 'ctrl',
bindings: {
item: '<',
previousParams: '<',
tipos: '<',
genero: '<',
assunto:'<',
ciudades: '<',
pais: '<',
folha: '<',
}
});
/* @ngInject */
function Controller($timeout, $state, ModalService , CIUDAD_FORM_ADMIN, Pais, PAIS_FORM_ADMIN) {
var vm = this;
$timeout(function () {
angular.element('.form-group:eq(0) input').focus();
});
vm.mode = $state.current.data.mode;
vm.canSave = vm.mode === 'create' || vm.mode === 'edit';
// Guardar formulario
vm.save = function () {
vm.item.$save().then(function () {
ModalService.closeComponent(vm.item, 'admin/prototexto/list', vm.previousParams);
});
};
vm.openCiudadModal = function () {
ModalService.openComponent('admin.ciudad.create.title', CIUDAD_FORM_ADMIN, {size: 'lg'}).result.then(function (result) {
// Añadimos el nuevo elemento al select
vm.ciudades.push(result);
// Asociamos el nuevo elemento a la entidad
vm.item.ciudades.push(result);
});
};
vm.openPaisModal = function () {
ModalService.openComponent('admin.pais.create.title', PAIS_FORM_ADMIN, {size: 'lg'}).result.then(function (result) {
// Asociamos el nuevo elemento a la entidad
vm.item.pais = result;
});
};
vm.paisService = Pais;
}
}) ();