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);
我可以使用查询从IFactory检索数据并在type_content中保存响应。现在我要做的是type_content中的数据我想保存它的usertype,因为用于type_content和usertype的模式是相同的。 请注意,来自IFactory的数据使用了嵌入式模式(使用数组)。
答案 0 :(得分: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
服务会在调用执行onSuccess2
函数的UserFactory.save
函数之前等待。如果出现错误,$q
服务将跳过onSucess2
功能。