2016-08-04 2 views
0
IFactory.query(
    function (response) { 
     $scope.type_content = response; 
     $scope.showMenu = true; 
    }, 
    function (response) { 
     $scope.message = "Error: " + response.status + " " + response.statusText; 
    } 
); 

$scope.saving = { 
    firstname: "how", 
    lastname: "are you", 
    type_num: "1", 
    usertype: [] 
}; 
UserFactory.save($scope.saving); 

쿼리를 사용하고 type_content에 응답을 저장하는 IFactory에서 데이터를 검색 할 수 있습니다. 이제 type_content와 usertype에 사용되는 스키마가 동일하기 때문에 user_ype을 그대로 저장하고자하는 type_content에있는 데이터가 필요합니다. IFactory에서 오는 데이터에는 임베디드 스키마 (배열 사용)가 사용되었습니다.다른 공장에서 응답을 얻은 후 angularjs에 데이터를 저장하는 방법

+0

'Ifactory'는'ngResource' 객체입니까? – georgeawg

+0

그래, 우리 ngResource 사용하는 .factory ('IFactory입니다', [ '$ 자원', 'base을', 기능 ($ 자원, base을) { 반환 $ 자원 (base을 + "T1/: ID가", NULL, { '업데이트': { 방법 : 'PUT' } }), }]) –

답변

0

Ifactory.query$resource 개체를 반환하기 때문에이 코드는 연결하기 위해 $promise 속성을 사용할 수 있습니다. 체인 및 을 반환함으로써

IFactory.query().$promise.then(
    function onSuccess(typeContent) { 
     $scope.type_content = typeContent; 
     $scope.showMenu = true; 
     //return typeContent for chaining 
     return typeContent; 
}).catch(
    function (errorResponse) { 
     $scope.message = "Error: " + errorResponse.status + " " + errorResponse.statusText; 
     //throw to chain errorResponse 
     throw errorResponse; 
}).then(
    function onSuccess2(typeContent) { 
     $scope.saving = { 
      firstname: "how", 
      lastname: "are you", 
      type_num: "1", 
      usertype: typeContent 
     }; 
     UserFactory.save($scope.saving); 
}); 

상기 UserFactory.save 함수를 실행 onSuccess2 함수를 호출하기 전에 $q 서비스 대기한다. 오류가있는 경우 $q 서비스는 onSucess2 기능을 건너 뜁니다.

관련 문제