Share a json data using only a $ scope

0

Hello StackOverFlow members.

This is my code.

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

app.controller("ctrl", ['$scope', '$http', function ($scope, $http) {
    var promise = $http.get("url1")
        .then(function (response) {
            console.log(response);
            $scope.files = []
            return $http.get('url2', {
                params: {
                    id: response.data[0].Id
                }
            })
        })
    .then(function (response2) {
        console.log(response2);
        $scope.files = response2.data;
        return response2.data;
    })
}])

my HTML

div ng-app="app" ng-controller="ctrl">
<script type="text/ng-template" id="category">
    <a href="{{file.Url}}"><strong>{{file.Name}}</strong></a>
    <ul ng-if="(files | filter:{ParentId : file.Id}).length > 0">
        <li ng-repeat="file in files | filter: {ParentId : file.Id" ng-include="'category'"></li>
    </ul>
</script>
<ul class="btn-highlight plus">
    <li ng-repeat="file in files | filter: {ParentId : 0}" ng-include="'category'"></li>
</ul>
</div>

I have a question, ParentId is just a variable, ParentId = response.data.Id, how can I include in my $ scope.files the response and the response2 at the same time and show the response.data.Id in my html code .

    
asked by AverageGuy 20.06.2016 в 04:54
source

2 answers

1

What you could do is concatenate both answers.

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

app.controller("ctrl", ['$scope', '$http', function ($scope, $http) {
    $http.get("url1")
      .then(function (response) {
          $scope.files = []
          $http.get('url2', {
              params: {
                  id: response.data[0].Id
              }
          }).then(function (response2) {
              // Al finalizar la segunda petición
              // si response.data es el vector entonces quedaría así:
              $scope.files = response.data.concat(response2.data);
          });
    });
}]);
    
answered by 21.06.2016 в 01:52
0

Something like that?

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

app.controller("ctrl", ['$scope', '$http', function ($scope, $http) {
    $scope.parentId = null;
    var promise = $http.get("url1")
        .then(function (response) {
            console.log(response);
            $scope.files = [];
            $scope.parentId = response.data.id;
            return $http.get('url2', {
                params: {
                    id: response.data[0].Id
                }
            })
        })
    .then(function (response2) {
        console.log(response2);
        $scope.files = response2.data;
        return response2.data.map;
    })
}])

You can use $scope.parentId for what you need.

    
answered by 20.06.2016 в 16:47