Angular / js select option

1

Personal I have an angle and an html:

I have id that is in a variable ( input hidden ).

How can I compare the id of this select/option with the id that is in the contents of this variable? The list comes correctly but I am not able to make this comparison and set default id that comes from this variable.

html:

<div class="col-md-6 col-lg-4">
<script>console.log(obj)
document.getElementById('myField').value = obj;
</script>
<input type="text" id="myField" name="myField" value="" />
<select class="form-control" name="cadastro" ng-options="cadastro.id for cadastro in (cadastro| orderBy:'id' | filter:{active:true}) track by 'id'" ng-model="cadastro" ng-selected="id=myField">
</select>
</div>

js:

function loadCadastro() {

var req = gatewayClient.delivery('lista').request('listCadstro');
requestMonitor.add('cadastro', req);
gatewayClient.execute(req).then(function(result) {
$rootScope.cadastro = result.data.value;

})
}

Thank you in advance

    
asked by anonymous 19.12.2017 / 21:20

2 answers

0

Thanks for the reply, my friend.  But I still can not solve it.

No html:

<select class="form-control" name="cadastro" ng-options="cadastro.cadastroNumber for cadastro in (cadastro| orderBy:'id' | filter:{active:true}) track by simplified.id" ng-model="cadastro" ng-init="cadastro.id=cadastro[indexDefault]" id="cadastro">

Non-Angular:

function loadCadastro() {
    var req = gatewayClient.delivery('document').request('listActiveCadstro');
    requestMonitor.add('cmptRegisterCadastroGrid', req);
    gatewayClient.execute(req).then(function(result) {
        $rootScope.cadastro = result.data.value;
        $scope.activeSimplified = true;

         var obj = document.getElementById('myField').value;

         //Encontra o index do objeto que deve ser selecionado
           $rootScope.cadastro.map(function(value, index){
             if(cadastro.id == obj){
               alert(obj);
               $rootScope.indexDefault = index;
               return;
             }
           })

        if ($scope.simplified.length === 0) {
            messageBox.show('error', 'Erro:', 'Não existe.');
            $scope.activeSimplified = false;
        }
     })
}

What can I be doing wrong?

    
20.12.2017 / 13:36
0

So I understand you want to start <select> with a predefined value.

For this you can use ng-init in the tag, only you can not only use the value of id , or set the whole object, you need to find the index in the list that you used in ng-options so the angular scope will make the "reference" correct. I've created an executable example to make it easier, but I've used a few different names of yours, but I think you can get an idea. Any questions just call.

-

The first problem in the second example you posted is in ng-model you can not use the same name as the variable that received the list.

The second problem is in ng-options the part that says you need to pass the name of the list and name of the variable that will represent an item in the item in lista list, in your case, cadastro in cadastros .

And third problem, the ng-model of <select> of a list of objects must be an object, so that angular can track and select the item you need.

I have edited the example below according to your second example.

angular
  .module('myApp', [])
  .controller('MyController', ['$scope', function($scope){
  //Carrega o valor do input hidden
  var obj = document.getElementById('myField').value;

  //Altere esse array pela sua request
  $scope.cadastros = [
    {
      cadastroNumber: "Maçã",
      id: 10,
      active: true
    },
    {
      cadastroNumber: "Banana",
      id: 12,
      active: false
    },
    {
      cadastroNumber: "Abacaxi",
      id: 20,
      active: true
    },
    {
      cadastroNumber: "Melancia",
      id: 13,
      active: true
    }
  ];
    
  //Encontra o index do objeto que deve ser selecionado
  $scope.cadastros.map(function(value, index){
    if(value.id == obj){
      $scope.indexDefault = index;
      return;
    }
  })
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><bodyng-app="myApp">
  <div ng-controller="MyController">
    <input type="hidden" id="myField" name="myField" value="10" />
    <select name="cadastro" 
       ng-options="
        cadastro.cadastroNumber 
        for cadastro 
        in (cadastros|orderBy:'id' | filter:{active:true})
        track by cadastro.id
       " 
       ng-model="cadastroSelecionado" 
       ng-init="cadastroSelecionado=cadastros[indexDefault]" 
       id="cadastro">
        </select>
  </div>
</body>
20.12.2017 / 03:55