indusree g
indusree g

Reputation: 21

How to call other service from one service response in AngularJS?

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope, $http) {
    $http.get("myservice").then(function (response) {
        $scope.studentdata = response.data;
    });
});

Based on student data i need to call other service like student address.how to achieve i am new to angular js please help.

Upvotes: 0

Views: 913

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

You need to use promise chains to call the request after another

    function firstReq(){
        return $http({
            method: 'POST',
            url: 'http://localhost/api/users',
            headers: {'Content-Type': 'application/json'},
            data: JSON.stringify($scope.user)
        })
    }

    function secondReq(){
       return $http({
          method: 'POST',
          url : 'http://localhost/api/users/' +$scope.user_id + '/accessTokens',
       })
    }

    $scope.processform = function() {
         firstReq()
           .then( function( response )
            {
                console.log(response.data.user_id);
                $scope.user_id = response.data.user_id;
                return secondReq();
            })
            .then(function(response){
               console.log(response.data);
            })
  }

Upvotes: 1

Related Questions