2016-08-04 11 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のためのスキーマが同じであるため、そのままusertypeをそのまま保存したいというtype_contentにあるデータです。 IFactoryからのデータは、埋め込みスキーマ(配列の使用)を使用しています。他の工場からの応答を得た後、angularjsのデータを工場に保存する方法

+0

'Ifactory'は' ngResource'オブジェクトですか? – georgeawg

+0

はい、私たちはngResourceを使用しています .factory( 'IFactory'、['$ resource'、 'baseURL'、function($ resource、baseURL){ return $ resource(baseURL + "t1 /:id" { '更新':{ 方法: '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); 
}); 

$qサービスはUserFactory.save機能を実行onSuccess2関数を呼び出す前に待機します。エラーが発生すると、$qサービスはonSucess2機能をスキップします。

関連する問題