1.语法:
$resource(url,[paramDefaults],[actions],options);
详解:
(1)url:一个参数化的url模板
(2)paramDefaults:url参数的默认值
(3)actions: 用户对于resource行为的默认设置进行扩展的自定义配置的散列,该配置将会以$http.config的格式创建。
(4)options:扩展$resourceProvider行为的自定义设置
2.五种默认行为
{
"get":{method:"get"},
"save":{method:"save"},
"query":{method:"get",isArray:true},
"remove":{method:"delete"},
"delete":{method:"delete"}
}
get([params],[success],[error]);
save([params],postData,[success],[error]);
query([params],[success],[error]);
remove([params],postData,[success],[error]);
delete([params],postData,[success],[error]);
$save([params],[success],[error]);
$remove([params],[success],[error]);
3.demo
(function () {
angular.module("Demo", ["ngResource"])
.controller("testCtrl", ["$resource",testCtrl]);
function testCtrl($resource) {
var myResource = $resource("/url/_url", {}, {
myPost: {
method: "post",
url: "/newUrl/_newUrl",
params: { id: "4" },
interceptor: {
response: function (d) {
console.log(d);
},
responseError: function (d) {
console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
}
}
}
});
myResource.get({ id: "1" }, function (d) {
console.log(d);
}, function (d) {
console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
});
myResource.query({ content: "text" }, function (d) {
console.log(d);
}, function (d) {
console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
});
myResource.save({ text: "Hello World" }, { text: "Hello World" }, function (d) {
console.log(d);
}, function (d) {
console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
});
myResource.remove({ text: "Hello World" }, { text: "Hello World" }, function (d) {
console.log(d);
}, function (d) {
console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
});
myResource.delete({ text: "Hello World" }, { text: "Hello World" }, function (d) {
console.log(d);
}, function (d) {
console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
});
var newResource = new myResource();
newResource.$save({ id: "2" }, function (d) {
console.log(d);
}, function (d) {
console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
});
newResource.$remove({ id: "3" }, function (d) {
console.log(d);
}, function (d) {
console.log(d);//这里的是随便写的地址,所以执行了error里的函数,可打印看参数及结果
});
myResource.myPost();
};
}());
.