r007
r007

Reputation: 304

How to make a double GET on angular 1.x

I want to do a doble Get using the return of the first one as input for the second one, is there a way to do this?

ExpedienteService.getExpediente(id).then(function(data) {
        vm.expediente = data;
    }).then(AutoService.getAuto1a5(vm.expediente).then(function(data) {
        vm.autos = data;
      })
    );

thats what i got so far.

Upvotes: 0

Views: 46

Answers (2)

Jeff Howard
Jeff Howard

Reputation: 344

function yourNewFunction() {
    var deferred = $q.defer();

    ExpedienteService.getExpediente(id).then(
        function getExpSuccess(resp) {
            var vm = {
              expediente = resp.data;
            };
            AutoService.getAuto1a5(vm.expeidente).then(
                function getAutoSuccess(resp) {
                    vm.autos = resp.data;
                    deferred.resolve(vm);
                },
                deferred.reject
            }
        },
        deferred.reject
    );

    return deferred.promise;
}

Upvotes: 1

yBrodsky
yBrodsky

Reputation: 5041

ExpedienteService.getExpediente(id).then(function(data) {
  vm.expediente = data;
  return AutoService.getAuto1a5(vm.expediente);
}).then(function(data) {
   vm.autos = data;
});

You were close.

Say if you didn't want to save the first data into vm.experiente you could do.

ExpedienteService.getExpediente(id)
  .then(AutoService.getAuto1a5)
  .then(function(data) {
     vm.autos = data;
  })

Upvotes: 1

Related Questions