我想我不理解如何在RESTful api中完成POST.使用以下方法在Restangular中创建新对象时:
var user = {name: "John", id:"123"};
Restangular.one('building','5').post(user);
Run Code Online (Sandbox Code Playgroud)
我希望它将带有user值的$ _POST数组传递给url example.com/api/building/5
并知道它正在对正确的脚本执行POST请求,但$ _POST数组为空.我知道我做错了什么?
Restangular有没有内置的方法,我可以为ajax/$ http调用提供超时?
我如何使用Restangular为ajax/$ http调用编写一个包装到Retry by Timeout.一个工厂??
谢谢,莱娜.
使用restangular处理嵌套资源时的最佳做法是什么?即
Restangular.one("accounts", 1).one("projects", 1).get().then(function (project) {
project.getList("proofs").then(function(proofs){
project.proofs = proofs;
_.each(proofs, function(proof){
proof.comments = proof.getList("comments");
});
});
$scope.project = project;
Run Code Online (Sandbox Code Playgroud)
});
这将允许我很好地访问视图内的每个.
<li ng-repeat="proof in project.proofs">Total: {{proof.comments.length}}</li>
Run Code Online (Sandbox Code Playgroud)
如果我想要对项目进行更新,那么它会将所有内容发送到项目REST端点(包括所有校样和所有proofs.comments).
project.name = 'New Name!';
project.put();
Run Code Online (Sandbox Code Playgroud)
这让我觉得我必须实施一些错误的东西,并且冥想是一种更好的处理方法吗?
没有用这样的东西直接定义所有的$ scope(未经测试)?即
Restangular.one("accounts", 1).one("projects", 1).get().then(function (project) {
project.getList("proofs").then(function(proofs){
$scope.projectProofs = proofs;
_.each(proofs, function(proof){
$scope.proofComments[proof.id].push(proof.getList("comments"));
});
});
$scope.project = project;
Run Code Online (Sandbox Code Playgroud)
});
建议的最佳做法是什么?
我正在尝试使用restangular进行文件上传请求,我想在restangular中实现与下面相同的功能.但是,我不确定如何为此特定请求设置内容类型和transformRequest.如果我理解正确,setDefaultHeader会为所有后续请求设置它.还有其他方法吗?
myApp.service('$fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var filedata = new FormData();
filedata.append('file', file);
$http.post(uploadUrl, filedata, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(){
})
.error(function(){
});
}
}]);
Run Code Online (Sandbox Code Playgroud) 我来自Backbone,所以也许我的观点对此有偏见,但我很难看到在Angular中建模数据的最佳方法.双向数据绑定非常棒,但过去当我想拥有持久集合和模型类时我很困惑.
我习惯于能够定义一个集合,比如用户,然后只要我想用新模型更新它就能调用.fetch().我也可以在集合和每个模型上定义自定义方法.
var users = new UserCollection();
users.fetch();
users.doSomethingCustom()
users.at(0).doSomethingModel();
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经查看了Restangular和ngActiveResource,似乎没有人提供与我预期相同的功能.
有没有我遗失的东西,或者我正在以非角度的方式思考这个问题?
编辑:我最终使我自己的模型/集合非常类似于Backbone的,如果它可以帮助任何人:https://github.com/evanhobbs/angular-models
这是oneUrl功能的签名:oneUrl(route, url)
从文档中:
oneUrl(route,url):这将创建一个新的Restangular对象,它只是指向具有指定URL的一个元素的指针.
对我而言,Route当您为资源提供URL时,设置似乎没用.为什么它存在于参数列表中?为什么强制要求?它怎么用?
我正在使用AngularJS与RESTful Web服务进行交互,使用Restangular来抽象暴露的各种实体.这些实体中的一些是图像或文件,因此我需要能够使用Restangular的post动作在同一请求中发送二进制数据和文本字段.
如何使用Restangular将上传的图像或文件的数据发送到单个POST请求中的宁静Web服务?
在DRF中我已经将分页限制添加到100,'PAGINATE_BY': 100,
因为Restangular期望数组形式的结果,我必须在我的角度应用程序模块中使用以下元提取器函数
var app = angular.module("myapp", ["restangular"].config(function(
RestangularProvider){
RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
if (operation === "getList") {
var newResponse = response.results;
newResponse._resultmeta = {
"count": response.count,
"next": response.next,
"previous": response.previous
};
return newResponse;
}
return response;
});
});
Run Code Online (Sandbox Code Playgroud)
我的控制器看起来像
app.controller('DataCtrl',function($scope, Restangular){
var resource = Restangular.all('myapp/api/dataendpoint/');
resource.getList().then(function(data){
$scope.records = data;
});
}
Run Code Online (Sandbox Code Playgroud)
控制器中没有元信息,如果有超过100条记录,我该如何分页?
django pagination angularjs django-rest-framework restangular
发送Restangular POST后如何获取响应对象?
firstAccount.post("Buildings", myBuilding).then(function() {
console.log("Object saved OK");
}, function() {
console.log("There was an error saving");
});
Run Code Online (Sandbox Code Playgroud)
我正在尝试获取新的对象ID.
谢谢.
是否可以使用Restangular与2个不同的API?我想两者都有setBaseUrl().
angularjs ×10
restangular ×10
rest ×5
file-upload ×2
javascript ×2
backbone.js ×1
django ×1
pagination ×1
post ×1