我已经查看了有关stackoverflow的所有相关问题,但没有找到任何相关的答案。也许这是房间里的大象,只有我看不到但是在浪费了整整一天之后,我真的很感激,如果有人可以提供一些见解。 我有一个带有范围变量$ scope.fruits的控制器,它使用资源将数据发布到/ fruits(设想示例)
.controller('FruitsController', ['$scope', 'addFruitsFactory', 'sliceFruitsFactory', function($scope, addFruitsFactory, sliceFruitsFactory) {
$scope.fruits = {
seasonal: "",
imported: "",
exported: ""
}
btn_serveFruits = function() {
// user selects a list of fruits from a select control in html.
// $scope.fruits is successfully bound to this select
// $scope.fruits has a list of selected fruits
addFruitsFactory.save($scope.fruits).$promise.then(
function(response) {
console.log('fruits in scope ' + $scope.fruis);
// $scope.fruits is empty here. Is it possible to access
// $scope data here so it can be passed to the next factory?
sliceFruitsFactory.slice($scope.fruits);
},
function(response) {
$scope.message = "Error : " + response.status + " " + response.statusText;
}
);
}
}
是否可以在资源返回的成功承诺中访问$ scope.fruits? 非常感谢您的回复。谢谢!
答案 0 :(得分:0)
也许我没有正确理解你。使用资源后,promise将返回可用于填充范围的对象。
service.save($scope.fruits).$promise.then(function(data) {
$scope.fruits = data;
//$scope.fruits now has the data to send to another factory/service
});
答案 1 :(得分:0)
将方法'btn_serveFruits'写为范围方法,如下所示:
app.controller('FruitsController', ['$scope', 'addFruitsFactory', 'sliceFruitsFactory', function($scope, addFruitsFactory, sliceFruitsFactory) {
$scope.fruits = {
seasonal: "",
imported: "",
exported: ""
}
$scope.btn_serveFruits = function() {
// user selects a list of fruits from a select control in html.
// $scope.fruits is successfully bound to this select
// $scope.fruits has a list of selected fruits
addFruitsFactory.save($scope.fruits).$promise.then(
function(response) {
console.log('fruits in scope ' + $scope.fruis);
// $scope.fruits is empty here. Is it possible to access
// $scope data here so it can be passed to the next factory?
sliceFruitsFactory.slice($scope.fruits);
},
function(response) {
$scope.message = "Error : " + response.status + " " + response.statusText;
}
);
}
}