2
I am using angular to make a form that contains multiple checkboxs (using angular-material), this form sends the values to the controler, which are received as follows:
I need to take this data (all objects contain the same fields) and turn it into a JSON. Does anyone know how this can be done?
Code of my controller:
vm.cadastraGrupo = function (dados){
vm.disabled = true;
console.log(dados);
}
My HTML:
<form name="userForm" ng-model="vm.data" ng-submit="vm.cadastraGrupo(vm.data)">
<md-input-container class="col-xs-12 md-block">
<label>Nome</label>
<input ng-model="vm.data.nome_grupo_menu" required="">
</md-input-container>
<div class="col-md-3 col-sm-6 col-xs-12">
<label class="label_tinyMCE left_100">Leitura</label>
<md-checkbox aria-label="Select All"
ng-checked="vm.isCheckedLeitura()"
md-indeterminate="vm.isIndeterminateLeitura()"
ng-click="vm.toggleAllLeitura()"> Selecionar tudo
</md-checkbox>
<div ng-repeat="menus in vm.respostaListaMenusCadastrados">
<div style="display: none;">{{vm.data[menus.id_menu].id_menu_rel_permissao = menus.id_menu}}</div>
<input type="hidden" ng-model="vm.data[menus.id_menu].id_menu_rel_permissao">
<md-checkbox ng-checked="vm.existsLeitura(menus, vm.selectedLeitura)" aria-label="menus {{$index}}" ng-click="vm.toggleLeitura(menus, vm.selectedLeitura)" style="width: 100%;" ng-model="vm.data[menus.id_menu].read_permissao">{{menus.nome_menu_rel}}</md-checkbox>
</div>
</div>
</form>
var conteudoJson =
angular.toJson(variavel);
?– OnoSendai
I think the problem is the numbering that is, for example, "3":{"id_menu_rel_permissao":"3","read_permissao":true}, I would have to take that first 3, in case I would need to transform that variable to something like: var potato = [{'id': 3}];
– Bernardo Kowacic
This is the name of the property. You need to iterate over the items and transform, for example, into a collection/array.
– OnoSendai
Yes, I thought about creating an empty array and pushing it, only the problem is that I don’t have the length of the variable I get from HTML and therefore I have no way of knowing when to stop
– Bernardo Kowacic
for(var propriedade in objeto) {};
– OnoSendai
Hello, you can comment on this as an answer for me to give as sure, as this was the right solution. Thank you
– Bernardo Kowacic
Bernardo, I only provided the parts, you implemented the solution. = ) Describe your implementation, and mark it as correct.
– OnoSendai