29
我需要从rest API获取数据,产品id是url的一部分(而不是查询参数)。
工厂:
.factory('Products', ['$resource',
function($resource) {
return $resource('products/:productId', {
productId: '@id'
}, {
query: {
isArray: false
},
update: {
method: 'PUT'
}
});
}
])
控制器:
$scope.getProduct = function(id, from) {
$scope.product = Products.get({ id: id }, function(){
console.log($scope.product);
});
}
我的网址构建这样的:
/products?id=5426ced88b49d2e402402205
代替:
/products/5426ced88b49d2e402402205
任何想法为什么?
非常感谢,这就是它! :) – orszaczky 2014-10-19 17:16:56
那么'@'前缀值的目的是什么呢? “如果参数值以@为前缀,那么该参数的值将从'data'对象的相应属性中提取(在调用操作方法时提供)例如,如果'defaultParam'对象是'{someParam :'@someProp'}'那么'someParam'的值将是'data.someProp'。“听起来你应该能够传递一个'id'值,资源会用它来填充'productId'参数。但这种情况并非如此。 – 2015-11-13 21:42:18
谢谢!今天帮助我! – 2016-01-13 21:14:41