标签: angular-resource

如何使用angularjs资源处理分页和计数?

我必须为API输出JSON构建一个angularjs客户端,如下所示:

{
  "count": 10,
  "next": null,
  "previous": "http://site.tld/api/items/?start=4"
  "results": [
    {
      "url": "http://site.tld/api/items/1.json",
      "title": "test",
      "description": "",
      "user": "http://site.tld/api/users/4.json",
      "creation_datetime": "2013-05-08T14:31:43.428"
    },
    {
      "url": "http://site.tld/api/items/2.json",
      "title": "test2",
      "description": "",
      "user": "http://site.tld/api/users/1.json",
      "creation_datetime": "2013-05-08T14:31:43.428"
    },
    {
      "url": "http://site.tld/api/items/3.json",
      "title": "test3",
      "description": "",
      "user": "http://site.tld/api/users/2.json",
      "creation_datetime": "2013-05-08T14:31:43.428"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能将$resource它映射到这个?如果我使用isArray=false,我会将整个blob作为一个对象,可用于阅读,但我无法调用.put()它.如果我使用isArray,它只是不起作用.

有没有干净的方法来做到这一点?或者我应该回去使用$http

rest angularjs angular-resource

28
推荐指数
2
解决办法
2万
查看次数

AngularJS - 将$ resource.query与params对象一起使用

我正在努力寻找angular.js并找出一些记录较少的东西.

考虑一下 - 我在服务器上有一个搜索方法,它接受查询参数并返回搜索结果的集合,并响应 GET /search.json路由(Rails FWIW).

因此jQuery,示例查询将如下所示:

$.getJSON('/search', { q: "javascript", limit: 10 }, function(resp) {
  // resp is an array of objects: [ { ... }, { ... }, ... ]
});
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用angular实现它,并围绕它的工作原理.这就是我现在拥有的:

var app = angular.module('searchApp', ['ngResource']);

app.controller('SearchController', ['$scope', '$resource', function($scope, $resource){

  $scope.search = function() {
    var Search = $resource('/search.json');
    Search.query({ q: "javascript", limit: 10 }, function(resp){
      // I expected resp be the same as before, i.e
      // an array of Resource …
Run Code Online (Sandbox Code Playgroud)

query-parameters angularjs angular-resource

28
推荐指数
1
解决办法
8万
查看次数

如何取消$资源请求

我试图找出如何使用$ resource的timeout属性动态取消挂起的请求.理想情况下,我希望能够取消具有某些属性的请求(基于发送的参数),但似乎这可能是不可能的.与此同时,我只是尝试取消所有挂起的请求,然后重置超时承诺以允许新请求.

问题似乎是$ resource配置只允许一个静态的超时值承诺.如果我正在进行单独的$ http调用,那么我可以做到这一点是有道理的,因为我可以为超时传递新的承诺,但是如何才能为$资源工作呢?我在这里设置了一个示例plunker:http://plnkr.co/edit/PP2tqDYXh1NAOU3yqCwP?p = preview

这是我的控制器代码:

app.controller('MainCtrl', function($scope, $timeout, $q, $resource) {
  $scope.canceller = $q.defer();
  $scope.pending = 0;
  $scope.actions = [];
  var API = $resource(
    'index.html', {}, {
      get: {
        method: 'GET',
        timeout: $scope.canceller.promise
      }
    }
  )

  $scope.fetchData = function() {
    if ($scope.pending) {
      $scope.abortPending();
    }
    $scope.pending = 1;
    $scope.actions.push('request');
    API.get({}, function() {
      $scope.actions.push('completed');
      $scope.pending = 0;
    }, function() {
      $scope.actions.push('aborted');
    });
  }

  $scope.abortPending = function() {
    $scope.canceller.resolve();
    $scope.canceller = $q.defer();
  }
});
Run Code Online (Sandbox Code Playgroud)

现在,取消器在有待处理的请求时工作,但我似乎无法重置它 - 一旦一个请求被中止,所有将来的请求也将被中止. …

angularjs angular-resource

27
推荐指数
2
解决办法
2万
查看次数

使用DELETE请求发送正文的干净方法是什么?

我需要使用$ resource发送一个带有DELETE请求的请求体

我能看到这样做的唯一方法就是改变:

https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js

var hasBody = action.method == 'POST' || action.method == 'PUT' || action.method == 'PATCH';
Run Code Online (Sandbox Code Playgroud)

var hasBody = action.method == 'POST' || action.method == 'PUT' || action.method == 'PATCH' || action.method == 'DELETE';
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来覆盖这个?就像改变内容类型标题一样,您可以这样做:

$httpProvider.defaults.headers["delete"] = {'Content-Type': 'application/json;charset=utf-8'};
Run Code Online (Sandbox Code Playgroud)

或类似的东西......我用谷歌搜索了这个,但也许我错过了一些明显的东西(不是第一次).在此先感谢您的帮助.

javascript angularjs angular-resource

23
推荐指数
1
解决办法
3万
查看次数

如何在Angular中使用$ resource访问响应头?

我基本上这样称呼get请求:

var resource = $resource('/api/v1/categories/:id')

resource.get({id: 1}).$promise.then(function(data){
  console.log(data)
})
Run Code Online (Sandbox Code Playgroud)

这工作正常..但我如何获得响应标头?

http angularjs angular-resource

19
推荐指数
3
解决办法
2万
查看次数

Rails复制了资源中的参数

我正在使用Angular资源,我不明白为什么Rails重复参数并将其放在资源名称中.我只需要了解为什么会这样.

// post data
{"title":"asdsad"}

// rails parameters 
Parameters: {"title"=>"asdsad", "presentation"=>{"title"=>"asdsad"}}
Run Code Online (Sandbox Code Playgroud)

重复,因为欢迎来到Stackoverflow,你需要50点评论,所以这发生了.我真的很抱歉:AngularJS $资源发出额外的"注册"哈希?

ruby-on-rails angularjs angular-resource

18
推荐指数
1
解决办法
2107
查看次数

是否可以使用带角度$ http服务的参数化URL模板

我使用$ resource作为我的RESTful api,并且喜欢参数化的URL模板 'api/clients/:clientId'

这适用于CRUD操作.然而,我的一些api只是报告或只读端点,而不需要完整的RESTful处理.我觉得为那些人使用$ resource并使用$ http的自定义数据服务是有点过分.

唯一的缺点是我丢失了参数化的URL模板.我很乐意定义一个网址,'api/clients/:clientId/orders/:orderId'然后通过{ clientId: 1, orderId: 1 }.我意识到我可以动态构建url,但希望$ http支持参数化模板,我还没有找到它.

祝一切顺利

更新7/5

我在搜索中遗漏的这个词是'Interpolate'.当我搜索'angular $ http'中的url interpolation时,会出现更多信息.简短回答看起来是'不'$ http不支持url插值.然而,有一些相当简单的方法来实现这一点.

1.使用$ interpolate:

这里的文档$interpolate

var exp = $interpolate('/api/clients/{{clientId}}/jobs/{{jobId}}', false, null, true);
var url = exp({ clientId: 1, jobId: 1 });
Run Code Online (Sandbox Code Playgroud)


2.编写自己的url插值函数

Ben Nadel在这里有一个关于这个确切主题的精彩帖子.


3.从角度资源中窃取功能

查看angular-resource.js中 Route.prototype上的setUrlParams .这很简单.

使用$ interpolate的示例数据服务

(function () {
    'use strict';

    var serviceId = 'dataservice.jobsReports';

    angular.module('app').factory(serviceId, ['$http', '$interpolate', function ($http, $interpolate) {

        var _urlBase = 'http://localhost:59380/api';
        var _endPoints …
Run Code Online (Sandbox Code Playgroud)

resources http angularjs angular-resource

16
推荐指数
1
解决办法
3739
查看次数

angular http:如何用自定义标题调用图像?

在html视图中,图像显示如下:

<img ng-src="{{element.image.url}}"> 
Run Code Online (Sandbox Code Playgroud)

element.image.url指向一个网址:/rest_api/img/12345678.

这工作正常,显示图像.

现在,我添加身份验证:

在对用户进行身份验证之前,每个资源都会响应一个http错误401,图像也是如此.验证成功后,会将令牌放在自定义标头中,并与每个$ http请求一起发送,以允许访问资源:

$http.defaults.headers.common['Authorization'] = token; 
Run Code Online (Sandbox Code Playgroud)

这适用于加载$ resource的Json文件.但是在认证之后,到图像的直接链接仍然是401.

如何使用自定义标题调用图像?

或者关于我应该怎么做的任何建议.

javascript authentication angularjs angular-resource

15
推荐指数
3
解决办法
2万
查看次数

.save和$ save之间的差异在angularjs中的资源

我见过的代码,这两个电话$save,并save在角一个$资源.

有什么区别,你什么时候使用?

angularjs angular-resource

15
推荐指数
1
解决办法
1万
查看次数

为什么角度设置未定义为清空ng-model字段

我在Angular(1.2)和Laravel(4.2)中有简单的crud应用程序.对于简单的crud操作,我使用高效的Eloquent方法:

$product->fill(Input::all());
Run Code Online (Sandbox Code Playgroud)

从请求有效负载中获取所有字段,但是当我需要使用空字段更新模型时会出现问题.

在编辑操作中,我有一个表单,其中填充了我的服务的$ resource get方法的响应:

adminModule.factory 'Product', ($resource) ->
  $resource '/admin/products/:id', { id: '@id' }, {
    query: { method: 'GET', isArray: false }
    update: { method: 'PUT' }
  }
Run Code Online (Sandbox Code Playgroud)

控制器:

adminModule.controller 'ProductFormEditController', ($scope, Product, $stateParams) ->

  $scope.formData = Product.get({id: $stateParams.id})
Run Code Online (Sandbox Code Playgroud)

和HTML:

    <input  type="text" data-ng-model="formData.name" name="name" class="form-control" id="name"
                           placeholder="Nazwa" data-server-error required>
Run Code Online (Sandbox Code Playgroud)

如果我清除此字段值并提交$ scope.formData的值设置为undefined并且PUT请求中没有包含,那么Laravel模型填充方法无论如何都看不到该字段并且不设置为空验证的值,但采用模型的原始值.

问题是:如何从ngModel而不是undefined发送空字符串?

ps如果输入类型是Textarea,$ resource发送空""字符串: - /,所以我很困惑......

laravel angularjs angular-resource

13
推荐指数
2
解决办法
1万
查看次数