switch out of cycle for in angularjs

1

I have an arrangement in which each item has a status, for what I do is look for the GENERATED or PENDING states and depending on each search a number is assigned to the variable value and then used in a switch and depending on that value do certain operations.

Here's my code:

var myApp = angular.module('myApp',[]);

    //myApp.directive('myDirective', function() {});
    //myApp.factory('myService', function() {});

    function MyCtrl($scope) {
        $scope.name = [
            {"id":1, "estado":"NO_AUTORIZADO"},
          {"id":2, "estado":"PENDIENTE"},
          {"id":3, "estado":"NO_AUTORIZADO"},
          {"id":4, "estado":"AUTORIZADO"},
          {"id":5, "estado":"AUTORIZADO"},
          {"id":6, "estado":"NO_AUTORIZADO"},
          {"id":7, "estado":"AUTORIZADO"},
          {"id":8, "estado":"PENDIENTE"},
          {"id":9, "estado":"NO_AUTORIZADO"},
          {"id":10, "estado":"AUTORIZADO"}
        ];
        var valor = 0;
        angular.forEach($scope.name, function(item){
            if(item.estado === 'GENERADO'){
            valor = 1;               
          }else{
            if(item.estado === 'PENDIENTE'){
              valor = 2;
            }else{
              valor = 3; 
            }                                             
          }
        })
        switch(valor){
            case 1:
            console.log(valor);
            //hacer operaciones correspondientes
          break;
          case 2:
             console.log(valor)
           //hacer operaciones correspondientes
          break;
          case 3:
            console.log(valor+' no hay nada que hacer')
          break;
          default:
          return false; 
        }
    }

But my mistake is that he always gets the last value. How can I solve this problem? ... the idea is that I want to do it outside the For cycle.

in advance I thank you ....!

    
asked by Dimoreno 07.02.2017 в 05:07
source

3 answers

1

I think that the value variable must go inside the for, and the switch also, but it does not make sense that you want to execute the switch 1 only once, outside the for, and at the same time you dislike the one that remains with the last value. It is then on the subject that you say you want to do it outside of what I do not understand.

You can use a function, as the friend says, if you want to lighten the logic of the for, or if you want to leave that logic aside (undock the switch); however, the only way I see it is that you tried to assign the variable 'value' and it did not print what you expected inside the for, and that is because the variable is declared before the for, and the assignment, is in the field of for, which causes that in all iterations could always print the same last value.

For this you must change the value declaration, and relocate it in the for, that will cause the variable to be written or re-declared, in each iteration (I'm not entirely clear why it works like this, but it works in several languages and it has to do with the scope of declaration), and if you want to then access it in the field after the for, there you use a variable similar to the one you have now

The detail, subtle, but almost a trick, is that you must use "var value = ..." inside the for

var myApp = angular.module('myApp',[]);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {
    $scope.name = [
        {"id":1, "estado":"NO_AUTORIZADO"},
      {"id":2, "estado":"PENDIENTE"},
      {"id":3, "estado":"NO_AUTORIZADO"},
      {"id":4, "estado":"AUTORIZADO"},
      {"id":5, "estado":"AUTORIZADO"},
      {"id":6, "estado":"NO_AUTORIZADO"},
      {"id":7, "estado":"AUTORIZADO"},
      {"id":8, "estado":"PENDIENTE"},
      {"id":9, "estado":"NO_AUTORIZADO"},
      {"id":10, "estado":"AUTORIZADO"}
    ];
    //var valor = 0;//Ya no iría aquí
    angular.forEach($scope.name, function(item){
      var valor = 0;//pero aquí si funciona, debe tener var para que sea una instancia diferente en cada iteración

      if(item.estado === 'GENERADO'){
        valor = 1;               
      }else{
        if(item.estado === 'PENDIENTE'){
          valor = 2;
        }else{
          valor = 3; 
        }                                             
      }

        console.log(valor); // este varía para cada iteración
        procesarItem(valor);
        //si prefieres aquí llamas a una función que tenga el switch
    });

   function procesarItem(valor) {

    switch(valor){
        case 1:
        console.log(valor);
        //hacer operaciones correspondientes
      break;
      case 2:
         console.log(valor)
       //hacer operaciones correspondientes
      break;
      case 3:
        console.log(valor+' no hay nada que hacer')
      break;
      default:
      return false; 
    }
   }
}
    
answered by 07.02.2017 в 18:48
0

applying knowledge of javascript and not directly angularjs

  

You could create a function that has a callback parameter, and through   this will obtain the result value to be able to work it.

function prueba(retorno){
angular.forEach($scope.name, function(item){
            if(item.estado === 'GENERADO'){
            valor = 1;               
          }else{
            if(item.estado === 'PENDIENTE'){
              valor = 2;
            }else{
              valor = 3; 
            }                                             
          }
return retorno(null,valor);
        })


}

prueba(function(err, valor){
  switch(valor){
            case 1:
            console.log(valor);
            //hacer operaciones correspondientes
          break;
          case 2:
             console.log(valor)
           //hacer operaciones correspondientes
          break;
          case 3:
            console.log(valor+' no hay nada que hacer')
          break;
          default:
          return false; 
        }
})
        
    
answered by 07.02.2017 в 05:29
0

The reason is that there is only one variable value, in each iteration that variable changes value, and since the last iteration is neither pending nor generated, then it will always be worth 3, if you want to do it outside of the for you can declare value as an array, var valor = [] and then insert each value in the.

angular.forEach($scope.name, function(item){
        if(item.estado === 'GENERADO'){
        valor.push(1);               
      }else{
        if(item.estado === 'PENDIENTE'){
          valor.push(2);
        }else{
          valor.push(3); 
        }                                             
      }
    })

or even add them to your original arrangement

angular.forEach($scope.name, function(item){
        if(item.estado === 'GENERADO'){
        item.valor = 1;               
      }else{
        if(item.estado === 'PENDIENTE'){
          item.valor = 2;
        }else{
          item.valor = 3; 
        }                                             
      }
    })

and then print them with a cycle

    
answered by 07.02.2017 в 15:30