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 publication is saved on the server, I need to call the UploadFileService
service to send an image or a file to the server, sending the code so that the image or file can be linked to its publication.
The idea is to make the service call UploadFileService
wait until the $ http returns with the publication. I started messing with JavaScript and AngularJS recently and I am not sure how to do chained promises (