How to store AJAX return to use as a parameter of another function

1

Main function, which will return the value chave = data.chave :

var chave = '';

$scope.submitForm = function() {
        $http({
          method  : 'POST',
          url     : 'validar.php',
          dataType: 'json',
          data : dados,
          headers : {'Content-Type': 'application/x-www-form-urlencoded'}
         })
         .success(function(data) {
            if (data.errors) {
              $scope.erro = data.errors.errovalidar;
            } else {
              chave = data.chave;
              $('#imprimir-danfe').removeAttr('disabled');
            }
          })
    };

Secondary function, which should take as the parameter the value returned from the main function ( chave ):

var imprimir_d = document.getElementById('imprimir');
    imprimir_d.addEventListener('click', function() {
        $http({
          method  : 'POST',
          url     : 'imprimir.php',
          data : chave,
          headers : {'Content-Type': 'application/x-www-form-urlencoded'}
         })
    });

However, the way it is, when I do console.debug(chave) , inside the secondary function and returned " undefined "

    
asked by anonymous 10.01.2017 / 03:44

2 answers

1

After $('#imprimir-danfe').removeAttr('disabled'); .... places: $('#imprimir-danfe').attr('meu_valor', chave );

And then just get the value of the key:

var imprimir_d = document.getElementById('imprimir');
    imprimir_d.addEventListener('click', function() {
        minha_chave = $('#imprimir-danfe').attr('meu_valor');
        $http({
          method  : 'POST',
          url     : 'imprimir.php',
          data : minha_chave,
          headers : {'Content-Type': 'application/x-www-form-urlencoded'}
         })
    });
    
10.01.2017 / 14:56
3

You can use $scope.chave , for angle it is better because it controls $scope .

I would also use the function of printing in angular calling through ng-click , and disable would use the variable itself of $scope.chave .

Make sure your key variable has data.

<br/>**javascript:**<br/>
$scope.submitForm = function() {<br/>
        $http({<br/>
          method  : 'POST',<br/>
          url     : 'validar.php',<br/>
          dataType: 'json',<br/>
          data : dados,<br/>
          headers : {'Content-Type': 'application/x-www-form-urlencoded'}<br/>
         })<br/>
         .success(function(data) {<br/>
            if (data.errors) {<br/>
              $scope.erro = data.errors.errovalidar;<br/>
            } else {<br/>
              $scope.chave = data.chave;<br/>
              $('#imprimir-danfe').removeAttr('disabled');<br/>
            }<br/>
          })<br/>
    };<br/>
$scope.imprimir = function() {<br/>
        $http({<br/>
          method  : 'POST',<br/>
          url     : 'imprimir.php',<br/>
          data : chave,<br/>
          headers : {'Content-Type': 'application/x-www-form-urlencoded'}<br/>
         })<br/>
    };<br/>
**html** <br/>

input type="button" ng-click="imprimir" ng-disabled="chave==undefined"
    
10.01.2017 / 15:26