Conditional ng-if cycle using http service on angularjs

1

I have an http service and with the code I have received 5 ids. I would like to call them using a scope using a cycle if , the json format has as property isFile that if it is false it is a folder and if it is true it is a file. My idea is to make a if that if it is false, make a repeating cycle that calls each id in an automated way.

A folder can have folders and files inside.

No more to say here is my code.

Controller

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

app.controller("ctrl", function ($scope, $http) {
    $scope.accessfolders = [];
    $scope.files = [];
    var promise = $http.get("(Primer-Link)primera-llamada-obtuve-un-id")
        .then(function (response) {
            console.log(response);
            return $http.get('(Segundo-Link)Use-el-id-de-la-primera-llamada-para-acceder-a-este-link', {
                params: {                          
                    id: response.data[0].Id //Obtuve el id de la primera llamada, Y llame a 3 carpetas y 2 archivos (quiero acceder a esas 3 carpetas usando un if)//
                }
            })
        })
    .then(function (response2) {
        console.log(response2);
        $scope.accessfolders = function () {
            if (response2.data.IsFile = false) //Tengo una propiedad llamada  IsFile, si IsFile = falso, el objecto es una carpeta//
                return $http.get('(Segundo-Link)Quiero-llamar-solo-las-carpetas-dentro-de-response2.data', { //fijaros que el primer link es para llamar el id de la carpeta principal y el segundo link para llamar a la carpeta que quiera usando su id//
                    params: {
                        id: response2.data[0].Id //No quiero usar [0] o [1], solo quisiera llamar cada que id obtuve con mi response2 de manera automatizada con un ciclo.//
                    }
                })
        }

        $scope.files = response2.data;
        return response2.data;
    })
})

HTML

<div ng-app="app" ng-controller="ctrl">
<table class="table">
 <tr ng-repeat="file in files" ng-if="file.isFile == accessfolders">
  <td>{{file.Name}}</td>
</tr>
</table>
</div>
    
asked by AverageGuy 17.06.2016 в 06:19
source

1 answer

1

You can run code using ng-init and ng-repeat with a filter to filter the objects that you do not want to process.

This is possible since ng-init and ng-repeat are executed with a priority of 450 and 1000 respectively. Always the highest priority directives are executed first so it is guaranteed that ng-repeat will be processed first.

I leave a demo of how it would be

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

app.controller("ctrl", function($scope, $http) {
  $scope.files = [];

  // Esta función se ejecutará por cada elemento filtrado de la lista
  $scope.callFile = function(file) {
    $http.get('(Segundo-Link)', {
      params: {
        id: file.Id
      }
    })
  };

  // Haces la primer llamada
  $http.get("(Primer-Link)").then(function(response) {
    // Haces la segunda llamada partiendo de los resultados de la segunda
    return $http.get('(Segundo-Link)', {
      params: {
        id: response.data[0].Id
      }
    })
  }).then(function(response) {
    // Pones todos los resultados en una variable del scope
    $scope.files = response.data
  }).catch(function() {
    // Manejo de errores
  });

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <table class="table">
    <tr ng-repeat="file in files | filter : {IsFile: true}" ng-init="callFile(file)">
      <td>{{file.Name}}</td>
    </tr>
  </table>
</div>
    
answered by 22.06.2016 в 14:25