6
I own a array called agenciasUri
, which format the data before inserting into it. After that, I mount JSON jsonObj
to send a request. What I found strange is that it is working properly.
Why can I access agenciasUri
? How the visibility of a variable within a Javascript loop works)?
$scope.salvar = function () {
var agenciasSeparadas = $scope.opcoes + '';
agenciasSeparadas = agenciasSeparadas.split(',');
try {
var agenciasUri = [];
for (var i = 0; i < $scope.listaAgencias.length; i++) {
var json = $scope.listaAgencias[i];
for (var k = 0; k < agenciasSeparadas.length; k++) {
if (json.nome == agenciasSeparadas[k]) {
agenciasUri.push(json._links.self.href);
}
}
}
} catch (err) {
alert(err);
}
var jsonObj = {
nome: $scope.nome,
tipo: $scope.tipo,
agencias: agenciasUri
};
alert(JSON.stringify(jsonObj));
veiculosAPI.postVeiculo(jsonObj)
.success(function (data, status, headers, config) {
$scope.nome = null;
$scope.tipo = null;
}).error(function (data, status, headers) {
alert("Erro ao salvar dados do veiculo!");
alert(data + "\t" + JSON.stringify(data));
});
};