我希望以符合DRY原则的方式将以下URL的工厂/服务调用添加到AngularJS项目中。该项目使用ngResource
。
http://localhost/vehicles/{type:car|truck}/drive/{2wd|4wd}?sort={"start":"1", "limit':"10", "sortBy": "make"}
http://localhost/vehicles/{type:car|truck}/drive/{2wd|4wd}/count
http://localhost/vehicles/bestselling/{type:car|truck}?sort={"start":"1", "limit':"10", "sortBy": "make"}
http://localhost/vehicles/bestselling/{type:car|truck}/count
"{type:car|truck}
“可以是"car"
之一,也可以是"truck"
"{2wd|4wd}"
。如何在Angular中定义这些资源的工厂/服务调用?我没有运气找到答案;我发现的最近的是this
免责声明:我没有AngularJS的经验
答案 0 :(得分:0)
我的解决方案:
services.js
resources.factory('Constants', [
function() {
return {
RESOURCE_URL: "http://localhost/vehicles"
}
}
]);
resources.factory('Rest', ['Constants', '$resource', function(C, $resource) {
return {
BestSellingCount: $resource(C.RESOURCE_URL + '/bestselling/:type/count', {
type: '@type'},{})
BestSelling: $resource(C.RESOURCE_URL + '/bestselling/:type', {
type: '@type'}, {
getBestSelling: { method:'GET', params: {sort: {"start":"1", "limit':"10", "sortBy": "make"}}, isArray: true)}}})
}]);
在我的控制器中
$scope.bestsellingtrucks = Rest.BestSelling.getBestSelling({type:"truck"});
默认排序和分页值作为查询字符串传递