【发布时间】:2014-05-20 02:53:46
【问题描述】:
我在服务器端使用 WebAPI:
public int Get(int productId)
{
//removed the actual logic to simplify the example
return 101;
}
角度:
$scope.showDetails = function (product) {
$scope.selectedProduct = product;
var queryArgs = { productId: product.id };
$scope.averageQuantity = Quantity.query(queryArgs, function() {
//callback function
console.log($scope.averageQuantity); // this shows a promise instead of an actual object
//and then open modal and pass the $scope as a parameter
});
};
//the resource:
.factory('Quantity', ['$resource', function ($resource) {
return $resource('/api/quantity', {}, { 'query': { method: 'GET', isArray: false } });
}])
我看到的不是数字 101:{"0":"1","1":"0","2":"1"}
如何实现回调才能看到对象而不是承诺?
【问题讨论】:
-
看来您在此问题中犯了与 OP 类似的错误:stackoverflow.com/questions/23749148/…(参见:RobM 的回答)您需要使用 @ 而不是尝试将回调作为参数传递,而是987654324@,如
$scope.averageQuantity = Quantity.query(queryArgs).then(function(results){ /* console.log(results.data */ }) -
感谢您的回复,马克。我认为 then() 方法是 jQuery 特定的方法,我没有在这个项目中使用 jQuery(我被要求不要)。我查看了那个链接,我确实错过了一个参数(用于发布数据),它就在成功函数之前。不幸的是,它仍然不起作用。这让我觉得这实际上可能是 $resource 错误配置而不是回调问题?这是我尝试过的:
$scope.averageQuantity = Quantity.query(queryArgs, {}, function(returnValue, responseHeaders) { console.log(returnValue); }); -
then不是 jQuery 特定的。 Angular 包含 $q,以 Q 为 promises 的模式,其中包含一个then方法。不过,我必须承认我最初的判断是错误的。该问题与回调或承诺无关。我即将发布的答案将说明问题所在。
标签: angularjs asp.net-web-api callback angularjs-factory