我喜欢该query()方法返回资源数组的方式,可以再次保存到服务器.
我正在尝试使用Angular来对抗Drupal RestWS模块,该模块返回一个具有多个"元"属性的对象和一个名为list的属性,其中存储了实际数据.有没有办法告诉资源采取该阵列?
示例:GET author.json返回:
first: "http://dgh/author?page=0"
last: "http://dgh/author?page=0"
list: [{id:1, type:author, uid:{uri:http://dgh/user/1, id:1, resource:user}, created:1367770006,…},…]
self: "http://dgh/author"
Run Code Online (Sandbox Code Playgroud) 我想提供服务提供资源,如下面的代码:
angular.module('myApp.userService', ['ngResource'])
.factory('UserService', function ($resource)
{
var user = $resource('/api/user', {},
{
connect: { method: 'POST', params: {}, isArray:false }
});
return user;
}
Run Code Online (Sandbox Code Playgroud)
然后在使用connect动作时,我想动态传递HTTP标头,这意味着它可能会针对每个调用进行更改.这是一个例子,在控制器中,请参阅代码中的注释:
$scope.user = UserService;
$scope.connect = function ( user )
{
var hash = 'Basic ' + Base64Service.encode(user.login + ':' + user.password);
// I would like this header to be computed
// and used by the user resource
// each time I call this function
$scope.user.headers = [{Authorization: hash}];
$scope.user.connect( {},
function()
{ …Run Code Online (Sandbox Code Playgroud) 在angularjs资源中,我想将我的json数据转换为JS对象
//Complex object with inheritance chain
function Car(year, make){
this.make = make;
this.year = year;
}
var carResource = $resource("/api/car/id", {id: '@id'},
{
get: {
method: 'GET',
transformResponse: function(data, headersGetter){
return new Car(data.make, data.year);
}
}
}
)
然而,这似乎并没有发生
我得到的是一个$resource对象,意味着属性make和year设置正确,但返回的对象的原型指向$resource
有没有办法可以将我的json数据直接映射到我自己的对象?
或者我是否必须编写自己的"资源"实现?
99%的ajax调用需要特定的"X-API-TOKEN"来验证我的Rails REST API并与之通信.但我也打电话给一个第三方API,我不断收到错误消息"Access-Control-Allow-Headers不允许请求头字段X-API-TOKEN".
如果我在调用之前删除标题,一切正常,并且解决方法是删除然后在调用之后重新添加,但是有一个比这更简单的方法:
apiToken = $http.defaults.headers.common["X-API-TOKEN"]
delete $http.defaults.headers.common["X-API-TOKEN"]
$http(
method: "GET"
url: 'http://...}}'
).success((data, status, headers, config) ->
).error (data, status, headers, config) ->
$http.defaults.headers.common["X-API-TOKEN"] = apiToken
Run Code Online (Sandbox Code Playgroud) 我试图使用来自自定义服务的数据从角度$ resource中装饰返回的数据.我的代码是:
angular.module('yoApp')
.service('ServerStatus', ['$resource', 'ServerConfig', function($resource, ServerConfig) {
var mixinConfig = function(data, ServerConfig) {
for ( var i = 0; i < data.servers.length; i++) {
var cfg = ServerConfig.get({server: data.servers[i].name});
if (cfg) {
data.servers[i].cfg = cfg;
}
}
return data;
};
return $resource('/service/server/:server', {server: '@server'}, {
query: {
method: 'GET',
isArray: true,
transformResponse: function(data, header) {
return mixinConfig(angular.fromJson(data), ServerConfig);
}
},
get: {
method: 'GET',
isArray: false,
transformResponse: function(data, header) {
var cfg = ServerConfig.get({server: 'localhost'});
return mixinConfig(angular.fromJson(data), ServerConfig); …Run Code Online (Sandbox Code Playgroud) 默认情况下,$resource.query()设置为期望成为$resource对象的对象数组.为了以一种漂亮,安静的方式适应分页,我将GET /api/widgets端点设置为返回以下对象:
{
currentPage: 1,
perPage: 20,
totalItems: 10039,
items: [{...}, {...}, {...}]
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使它成为angular将知道items属性是作为$resource对象的项目数组?
在Angular JS中是否有相当于Backbone的Collection或Ext JS的商店?我正在学习$ resource,但并没有完全掌握这方面的内容.
// This is the "collection" I'm interested in.
$scope.foos = [];
// Foo is a $resource.
Foo.query(function (foos) {
// This works, but is there a smarter "collection" object?
$scope.foos = foos;
});
$scope.createFoo = function (data) {
var foo = new Foo(data);
foo.$save(function (shinyNewFoo) {
$scope.foos.unshift(shinyNewFoo);
});
};
Run Code Online (Sandbox Code Playgroud)
在$scope.createFoo我制作一个新的Foo,坚持它,然后将它添加到我的穷人的集合,一个阵列.这工作正常,视图正确更新,但我太懒了.理想情况下,我想要一个我可以添加和删除的"集合"将自动POST/DELETE.也许类似以下内容.
$scope.fooCollection = new CoolCollection({
// specify the type of resources this collection will …Run Code Online (Sandbox Code Playgroud) javascript angularjs backbone.js-collections extjs-stores angular-resource
我知道一个类似的问题:Pdf.js:使用base64文件源而不是url呈现pdf文件.这个问题得到了Codetoffel的极好回答,但我的问题不同之处在于我的PDF是通过RESTful调用我的Web API实现来检索的.让我解释...
首先,这是使用PDF.JS通过URL打开PDF的基本方法:
PDFJS.getDocument("/api/path/to/my.pdf").then(function (pdf) {
pdf.getPage(1).then(function (page) {
var scale = 1;
var viewport = page.getViewport(scale);
var canvas = document.getElementById('the-canvas');
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
page.render({canvasContext: context, viewport: viewport});
});
});
Run Code Online (Sandbox Code Playgroud)
这很好用,但我使用Angular及其$resource服务通过我的RESTful Web API发出PDF请求.我知道PDF.JS让我来代替传递URL作为PDFJS.getDocument方法(上图)的字符串中包含DocumentInitParams的对象,它被定义在这里.使用DocumentInitParams对象的工作方式如下:
var docInitParams = {
url: "/api/path/to/my.pdf",
httpHeaders: getSecurityHeaders(), //as needed
withCredentials: true
};
PDFJS.getDocument(docInitParams).then(function (pdf) {
...
});
Run Code Online (Sandbox Code Playgroud)
这也有效,但它可以$resource通过要求我构建api URL来解决我的Angular问题.但这没关系,因为PDFJS允许我直接给它PDF数据,而不是给它一个PDF的URL,如下所示:
var myPdf = myService.$getPdf({ Id: 123 });
//It's …Run Code Online (Sandbox Code Playgroud) 我有这个终点
/clients/:id
/bills/:id
/clients/:id/bills
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用angular-resource创建一些资源来表示我的API.
我为客户创建了一个资源,
.factory('Clients', function($resource){
return $resource('/clients/:id')
})
.factory('Bills', function($resource){
return $resource('/bills/:id')
});
Run Code Online (Sandbox Code Playgroud)
工作得很好.
我的问题是当我想定义一个资源来表示调用端点的客户端的账单时 /client/:id/bills
我认为这应该是一个带有方法或类似方法的Bills资源getFromClient(),因为它将从客户端返回一个Bills数组.但我已经使用了比尔名称.端点与已定义的端点不同.
知道如何构建这个吗?
我有一个angularJS $资源:
$resource("http://localhost:3000/:id",{
id: '@id'
},
{
get: {
method:'GET',
isArray: false
},
foo: {
method:'POST',
url: 'http://localhost:3000/:id/foo',
isArray: false
}
});
Run Code Online (Sandbox Code Playgroud)
现在,如果我打电话:
User.foo({id:'123', anotherParam: 'bar'});
Run Code Online (Sandbox Code Playgroud)
这会导致调用URL" http:// localhost:3000/foo "并将id和anotherParam参数作为POST字段传递.
我实际上希望它调用' http:// localhost: 3000/123 / foo '并仅将anotherParam参数作为POST字段传递.
如何让id参数正常运行?