0

I am trying to combine response of one POST data with another input JSON so that it can do another POST function.

Controller 1:-

$scope.master = {};

     $scope.addChild = function(net) {
     $scope.master = angular.copy(net);
     $http.post("url",$scope.master).then( function(data) { 
    $scope.childData = data;
    $scope.dataName = $scope.master.Name;
         growl.success("Created a new Child");
         console.log(data);

     },function (data, status, headers, config) { 
           growl.error("Something went wrong");
    });
           console.log($scope.master);
};

Now I want to add the response of this controller ie; $scope.segments = data; to 2nd controller:-

Controller 2:-

$scope.addOverall = {Child: []};        
             $scope.Create = function() {
         $http({
           method : 'POST',
           url : 'api',
          data : $scope.addOverall
        }).success(function(data, status, headers, config) { 
           growl.success("Created a Table");    
            console.log(data);
        },function (data, status, headers, config) { 
           growl.error("Something is wrong");

     });

};

So now , my response data from controller 1 must combine with 2nd controller input over here {Child: []}; inside the [ ] How can I achieve that?

WhoAmI
  • 217
  • 1
  • 2
  • 23
  • how about create a global variable and append the JSON for two post – Lokesh Pandey Apr 23 '18 at 08:53
  • ^ you mean [sharing the data between controllers with a service](https://stackoverflow.com/questions/21919962/share-data-between-angularjs-controllers)? – Aleksey Solovey Apr 23 '18 at 08:55
  • @Lokesh right , for global variable $rootScope will be bad practise , using service I can achieve that , but how will I push the response to service and fetch only in this portion `[ ]` ? – WhoAmI Apr 23 '18 at 08:56
  • @AlekseySolovey exactly – Lokesh Pandey Apr 23 '18 at 08:57
  • @WhoAmI read the Aleksey comment – Lokesh Pandey Apr 23 '18 at 08:58
  • @AlekseySolovey Yeah kind of , my concern is how to pass the response data exactly inside this {Child: `[ ]`}; – WhoAmI Apr 23 '18 at 08:59
  • @WhoAmI you don't need to append the result of one post in a service's global variable and then append the result of another post in the same variable – Lokesh Pandey Apr 23 '18 at 09:00
  • @WhoAmI can you pass the promise from a service? e.g. `return $http.post("url", master).then((data) => {return data.data;})` and receive in another controller with `SharedService.postData(master).then((res)=>{$scope.addOverall.Child = res;})` – Aleksey Solovey Apr 23 '18 at 09:04
  • @Lokesh I am just trying to append result of one POST that's it and too in the raw body for 2nd controller. – WhoAmI Apr 23 '18 at 09:05
  • Aleksey has just wrote an answer for you – Lokesh Pandey Apr 23 '18 at 09:06
  • @AlekseySolovey Can you show me a work around. I never tried to post something from service. I need to check. – WhoAmI Apr 23 '18 at 09:08
  • @AlekseySolovey I tried but how will I push master in url in service , as master is user input json. ? – WhoAmI Apr 23 '18 at 10:03
  • @WhoAmI possible with [this method](https://stackoverflow.com/questions/21855697/sharing-data-between-controllers-using-promises) – Aleksey Solovey Apr 23 '18 at 10:16
  • @AlekseySolovey This one is for GET which I am pretty familiar with in service. But for POST , I need to add raw body which master from click function. – WhoAmI Apr 23 '18 at 10:21

1 Answers1

0

Controller 1:

If you get result in data then use $rootscope for other variable

$rootScope.myGlobalVariable = [] //if response is array or object

$http.post("url",$scope.master).then( function(data) { 
$rootScope.myGlobalVariable = data;

Controller 2:

success(function(data, status, headers, config) { 
$scope.mylocalVariable = data;

final values in controller 2

for(var i=0; i<$scope.localVariable.length; i++)
{
  $rootScope.myGlobalVariable.push($scope.localVariable[i]);
}
console.log('combine json values', $rootScope.myGlobalVariable);

I hope this answer is work for You. Thanks

Shubham Kandiyal
  • 340
  • 2
  • 13
  • Valuable suggestion , Thanks . I solved this problem already. Though I have to push in same controller function not in other. – WhoAmI May 02 '18 at 06:20