Problem with ng-checked Angularjs!

Asked

Viewed 515 times

1

Person have an input from type="radio" when clicked calls a function and fills a select.. but this only happens when I click the input.. I need it to load automatically on the page loading, I have the following code:

<label class="margin-10-right">
   <input data-ng-click="vm.getFinanceiroTipoReceita(); vm.despesa.TipoSaida = false; vm.despesa.TipoEntrada = true"
       data-ng-checked="!vm.despesa.TipoEntrada"
       data-ng-value="true"
       data-ng-model="vm.despesa.TipoEntrada"
       name="tipo"
       type="radio">
       Entrada (Receita)
</label>

function getFinanceiroTipoReceita() {
  vm.listaArray = [];
  $.each(vm.listaTipoDespesa, function (indice, obj) {
    if (obj.TipoEntrada == true) {
      vm.listaArray.push(obj);
      // console.log(vm.listaArray);
    }
  });
}
  • Add the code of your controller.

  • Ready already added

  • You want to fill the select in accordance with the radio that was marked, that?

  • It already happens, but only when I click on radio.. I need you to do this when the page is loaded and the radio is already checked as default...

  • 1

    Just you assign on JS the value in the variable related to ng-model and call the function that is in the ng-click.

  • Has any response helped solve the problem and can address similar questions from other users? If yes, do not forget to mark the answer as accepted. To do this just click on the left side of it (below the).

Show 1 more comment

1 answer

1

Your problem is somewhat confused, but follows an example using a function _ inicializar which allows the selection and filtering to be performed immediately upon starting the controller:

angular
  .module('meuApp', [])
  .controller('MeuController', MeuController);

MeuController.$inject = ['$filter'];

function MeuController($filter) {
  var vm = this;
  var _opcoes;
  
  vm.filtrar = _filtrar;
  vm.selecionado = {};
  
  _inicializar();

  function _inicializar() {
    vm.tipos = [];
    
    vm.tipos.push({descricao: 'Débito', tipo: 'D'});
    vm.tipos.push({descricao: 'Crédito', tipo: 'C'});
    
    _opcoes = [];
    _opcoes.push({descricao: 'Conta Poupança', tipo: 'C', codigo: 1});
    _opcoes.push({descricao: 'Conta Corrente', tipo: 'D', codigo: 2});
    _opcoes.push({descricao: 'Dinheiro', tipo: 'C', codigo: 3});
    _opcoes.push({descricao: 'Dinheiro', tipo: 'D', codigo: 4});
    _opcoes.push({descricao: 'Vale Compra', tipo: 'C', codigo: 5});
    _opcoes.push({descricao: 'Cartão de Crédito', tipo: 'D', codigo: 6});
    _opcoes.push({descricao: 'Vale Alimentação', tipo: 'D', codigo: 7});
    _opcoes.push({descricao: 'Vale Refeição', tipo: 'D', codigo: 8});
    
    vm.selecionado.tipo = 'D';
    vm.filtrar();
  }
  
  function _filtrar() {
    vm.opcoes = $filter('filter')(_opcoes, {tipo: vm.selecionado.tipo});
    vm.selecionado.opcao = vm.opcoes[0].codigo;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="meuApp" ng-controller="MeuController as vm">
  <label ng-repeat="tipo in vm.tipos">
    <input type="radio"
           ng-model="vm.selecionado.tipo"
           ng-change="vm.filtrar()"
           ng-value="tipo.tipo"/>
    {{tipo.descricao}}
  </label>
  <br>
  <br>
  <select ng-options="opcao.codigo as opcao.descricao for opcao in vm.opcoes"
      ng-model="vm.selecionado.opcao"></select>
</div>

Browser other questions tagged

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