Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
How to the objects to perform calculation in angularjs

I am using angularjs in my project.

I am able to fetch the records from Database and binding in html page.Here I need to get the data from 4 collections in database so I need to perform several server calls to get the data. When i am assigning everything in separate Scope variables. My sample code is below

var click = function(){    
     $http.get('/CalBuildingget').then(function (response) {                    
                    $scope.ViewBuildings = response.data;

                    });

     for (i = 0; i < $scope.ViewBuildings.length; i++) {
        $http.get('/CalBuildingFloorget/'+ scope.ViewManageBuildings[i]._id).then(function (response) {                                
                    $scope.floorDetails = response.data;        
                        });
                     }

Here I need to fetch floors for each Building by its Id and store in building scope as an array object and then by floor id fetch again units which again needs to do server calls and assign inside the scope.

How can I achieve that as first it performs the loop then it starts server call of building.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You need to fetch floors in success callback of first request. So something like this.

var click = function(){    
 $http.get('/CalBuildingget').then(function (response) {                    
                $scope.ViewBuildings = response.data;
                for (i = 0; i < $scope.ViewBuildings.length; i++) {
                  $http.get('/CalBuildingFloorget/'+  scope.ViewManageBuildings[i]._id).then(function (response) {                                
                $scope.floorDetails = response.data;        
                    });
                 }
                });
over 4 years ago · Santiago Trujillo Denunciar

0

You'll mess up the whole performance of your application with approach you are using, are you sure that you want to send HTTP call in loop? think of a case when you have around 1000 records, shall you afford to send 1000 HTTP calls to server? instead why don't you fetch floorDetails in /CalBuildingget/ ?

Never send HTTP calls in loop, think of network bandwidth and application performance.

over 4 years ago · Santiago Trujillo Denunciar

0

For multiple subsequent service calls you should always utilise promise concept. conceptually it should be like below:

function callServiceForEachItem() {
  var promise;

  angular.forEach(items, function(item) {
    if (!promise) {
      //First time through so just call the service
      promise = fakeService.doSomething(item);
    } else {
      //Chain each subsequent request
      promise = promise.then(function() {

        return fakeService.doSomething(item);
      });
    }
  });
}

use this link for best practice perform chain service call

you can check this discussion

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda