5
I’m having trouble with the following code:
$http({
method: 'POST',
url: '/publicacao/',
data: $scope.publicacao,
headers: {'Content-Type': 'application/json'}
}).success(function(publicacao) {
$scope.publicacao = publicacao.data;
console.log($scope.publicacao);
//Upload de imagem
if ($scope.tipo == 'imagem') {
var arquivo = $("#imagem")[0].files[0];
var nomeArquivo = $("#imagem")[0].files[0].name;
UploadFileService.uploadArquivo('/upload', arquivo, nomeArquivo , publicacao.codigoPubl , 'imagem');
}
//Upload de arquivo
else if ($scope.tipo == 'arquivo') {
var arquivo = $("#arquivo")[0].files[0];
var nomeArquivo = $("#arquivo")[0].files[0].name;
UploadFileService.uploadArquivo('/upload', arquivo, nomeArquivo , publicacao.codigoPubl , 'arquivo');
}
$location.path('/timelines/' + $routeParams.codTime);
$window.location.reload();
$scope.addSucessoMsg();
}).error (function(erro) {
console.log('Erro ao adicionar a publicação: ' + erro);
$scope.addErroMsg();
});
After the post is saved on the server, I need to call the service UploadFileService
to send an image or file to the server, sending the code so that the image or file can be linked with the respective publication.
The idea is to make the service call UploadFileService
wait until $http returns with the post. I started messing with Javascript and Angularjs a little while ago and I’m having doubts about how to make chained Names (Synchronous requests), I read some posts in Portuguese and English but I don’t know how to use it to solve this problem.
If anyone can help, I’d appreciate it.
What problem are you having?
– novic
From what I understand, the
$http
makes an asynchronous call, which in theory allows the code to continue its execution until it finishes, right? The problem I imagine is this, it is possible somehow that the calls from the serviceUploadFileService
are executed before the$http
have received the response from the server? So far during my tests it has not occurred that the post will look likeundefined
and make a mistake, but I want to be able to prevent that from happening. I started reading about the papers a short time ago, so I still have doubts about how they work.– Rafael Lopes