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