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 shall receive as parameter the returned value of 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)
, within the secondary function and returned "undefined
"
Your code should work. Because it has a global variable that captures the value. Submit occurs before pressing the button with id "print"?
– mau humor
In the first script you did not set the key variable to global. Take the var and leave only: key = ';. Now it will work.
– Willian Coqueiro
Submit occurs before pressing the button with the id "print". @mauhumor
– lucasbento
The problem still persists. @Williancoqueiro
– lucasbento