2014-06-06 1 views
2

다음과 같은 작업을하려고합니다. 컨트롤러에서 $ recource 호출을 사용하여 데이터베이스에서 데이터를 가져 오는 함수가 있습니다. 서비스 '는 myService'

var fillSubData = function (containerToFill) { 
    resService.getSubDataFromDB(//$resource service 
    {params}, 
    function (res) { 
     //do something with containerToFill with the result res add new values 
    } 
); 
} 

var fillData = function (containerToFill) { 
    resService.getDataFromDB(//$resource service 
    {params}, 
    function (res) { 
     //do something with containerToFill with the result res 
     fillSubData(containerToFill); 
    } 
); 
} 

컨트롤러

$scope.dataToFill;// object 

var initialize = function() { 
    //by reference 
    myService.fillData(dataToFill); 
    // I need the dataToFill filled to do other thing with data recovered and built 
    angular.forEach(dataToFill.someArrayBuilt, function (item) { 
    //do something with item... 
    }) 
} 

I 복구 및 내장 데이터를 다른 일을 가득 dataToFill 필요하지만 자원 호출 asyn이다, 나는이 작업을 수행 할 수있는 방법?

답변

0

리소스 작업은 $promise 속성을 포함하는 개체를 반환합니다. 비동기 호출이 반환되면 당신은 콜백을 진행하려면이 옵션을 사용할 수 있습니다

myService.fillData(dataToFill).$promise.then(function() { 
    // I need the dataToFill filled to do other thing with data recovered and built 
    angular.forEach(dataToFill.someArrayBuilt, function (item) { 
     //do something with item... 
    }) 
}); 

이를 사용하려면, 당신은 단순히 당신의 fillData 방법은 자원 호출의 결과 반환이 제안 :

var fillData = function (containerToFill) { 
    return resService.getDataFromDB(//$resource service ...