2017-04-25 1 views
0

내 프로젝트에서 angularjs를 사용하고 있습니다.angularjs에서 계산을 수행하는 객체 방법

데이터베이스에서 레코드를 가져 와서 html 페이지에서 바인딩 할 수 있습니다. 데이터를 가져 오기 위해 여러 서버 호출을 수행해야하므로 데이터베이스의 4 개 컬렉션에서 데이터를 가져와야합니다. 모든 것을 별도의 범위 변수에 지정할 때. 내가 배열 개체로 범위를 구축 id를 저장하여 각 건물에 대한 바닥을 가져 오기 위해 필요하고 바닥 ID로 다시 서버 호출을하고 할당 할 필요가 다시 단위를 가져 오기 여기에

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;   
         }); 
        } 

아래 내 샘플 코드입니다 범위 안에.

먼저 루프를 수행 할 때 건물의 서버 호출을 시작하는 방법을 얻을 수 있습니다.

답변

0

첫 번째 요청의 성공 콜백에서 바닥을 가져와야합니다. 이렇게 뭔가가.

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;   
        }); 
       } 
       }); 
0

사용중인 접근 방식으로 응용 프로그램의 전체 성능을 망칠 수 있습니다. HTTP 호출을 루프로 보내시겠습니까? 약 1000 개의 레코드가있을 때 서버에 1000 개의 HTTP 호출을 보낼 여력이 있습니까? 대신에/CalBuildingget /에서 floorDetails를 가져 오지 않는 이유는 무엇입니까?

루프로 HTTP 호출을 보내지 마십시오. 네트워크 대역폭과 응용 프로그램 성능을 생각하십시오.

0

여러 개의 후속 서비스 호출의 경우 항상 약속 된 개념을 사용해야합니다. 개념적으로는 아래처럼한다 : 당신이 확인할 수 있습니다

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); 
     }); 
    } 
    }); 
} 

사용하는 가장 좋은 방법은 perform chain service call

이 링크 this 토론

관련 문제