标签: angular-resource

Angular $资源无法正确解析整数响应

我正在使用Angular $资源向我的控制器发出请求,在Spring应用程序上实现.当控制器只返回一个Integer值时,$ resource会解析它.使用Firebug进行检查我得到的结果如下:

Resource { 0="1", 1="9", 2="1", more...}
Run Code Online (Sandbox Code Playgroud)

其中191只是服务器返回的intger值.其他复杂对象没有问题(由服务器在JSON中解析).建议?谢谢FB

angularjs angular-resource

7
推荐指数
1
解决办法
4690
查看次数

如何配置Angular $ resource(ngResource)以使用CORS从另一个域提取数据

我希望能够使用CORS来设置资源,使用CORS来请求我的数据.我有CORS使用$ http,但相同的技术不适用于$ resource我希望有人可以来救我并告诉我如何使用$ resource.

我已经修改了Angular教程最后一步,通过在services.js文件中攻击phonecatServices服务来使用CORS.

我发现这个例子使用$ http.defaults.useXDomain = true; 删除$ http.defaults.headers.common ['X-Requested-With'];行获取使用CORS请求数据的角度但是如果我尝试$ resource.defaults.useXDomain = true; 我收到错误:"无法设置未定义的属性'useXDomain'".

我认为$ resource没有这个属性,所以我的问题是,如何使用CORS配置$ resource来发出跨域资源请求.

这是我的代码:

angular.module('phonecatServices', ['ngResource']).
    factory('Phone', function($resource){
  return $resource('http\\://localhost\\:8080/:phoneId.json', {}, {
    query: {params:{phoneId:'phones'}, isArray:true}
  });
});
Run Code Online (Sandbox Code Playgroud)

当我尝试发出请求时出现以下错误:对象#<资源>没有方法'推送'

编辑

我已经尝试设置$ http并且它大部分时间都可以工作,但是当调用资源查询时,在这种情况下是Phone.get(phoneId); 这似乎抛出了上述错误.

调用我怀疑导致错误的代码(来自controllers.js 第11步角度教程):

function PhoneDetailCtrl($scope, $routeParams, Phone) {
  $scope.phone = Phone.get({phoneId: $routeParams.phoneId}, function(phone) {
    $scope.mainImageUrl = phone.images[0];
  });

  $scope.setImage = function(imageUrl) {
    $scope.mainImageUrl = imageUrl;
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我删除上述方法的内部代码运行正常(没有获得网站的图像),但我不明白为什么这不起作用?我已经设置了$ http服务来使用CORS,所以应该将它传递给$ resource.

任何人都可以对此有所了解吗?(任何工作示例代码将不胜感激).

编辑:13/08/13

正如所有访问这个问题的人都知道的那样,下面的答案都没有真正回答过这个问题,我正在研究一个答案,但是如果有人发现这个并且有答案我会非常感激它.

编辑:06/09/13

目前正在调查这个项目,似乎允许我正在寻找的一切:https …

jsonp cors angularjs angular-resource angularjs-resource

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

如何在angularjs中设置条件$资源超时?

似乎很清楚如何设置一个全局方法来中止http请求.有点像

$scope.canceller = $q.defer();
var API = $resource('http:/foo.com', {}, {get: { 
    method:'GET',
    timeout:$scope.canceller.promise
}});

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

但是,我希望能够仅中止特定请求.我可以为我需要能够中止的请求定义单独的资源,但这非常难看.当我调用可以允许超时有条件的$ resource时,有什么方法可以传入参数或其他东西吗?也许是这样的:

var API = $resource('http:/foo.com', {}, {get: { 
    method:'GET',
    timeout:function() { if (FOO) { return $scope.canceller.promise; }}
}});
Run Code Online (Sandbox Code Playgroud)

但是如何访问FOO?

感谢您的任何见解!

angularjs angular-resource angular-http

5
推荐指数
1
解决办法
3418
查看次数

使用$ resource发送ETag

使用Angular JS的$ resource服务,有没有办法为资源的第二,第三等投票指定If-None-Match和ETag标头?

    var SummarySearch = $resource(<<apiurl>>,
        { },
        {
            get: {
                method: 'GET',
                headers: {
                    'x-header': 'id'
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

我已经得到这个来设置一个常量标题(这只是x-header: id在这种情况下),但我需要根据我上次看到的ETag的每个请求而变化的东西.

更新:

transformRequest看起来很有希望,但我无法访问我发起请求的数据,或者数据最终的URL.据我所知,我只能访问我正在POST的主体(在我做GET时未定义)和headersGetter函数.

rest angularjs angular-resource

5
推荐指数
1
解决办法
2277
查看次数

如何从角度资源获取完整的响应头

我正在发送一个Angular $资源的请求:

myResource.get({ foo: bar }, function success(data, headers) {
            console.log("data:", data); // Prints data
            console.log("headers:", headers()); // Prints only few headers
        }, function failure(response, status) {
            // Handling failure here...
        })
Run Code Online (Sandbox Code Playgroud)

但我只得到几个标题:

{content-type: "application/json", cache-control: "no-cache, max-age=604800", expires: "Mon, 06 Apr 2015 16:21:17 GMT"}
Run Code Online (Sandbox Code Playgroud)

当我想要抓住标题"X-Token"(如果我在浏览器控制台中检查就会收到)

有没有想过从Angular和$ resource接收完整的标题列表?

http-headers angularjs angular-resource

5
推荐指数
1
解决办法
1740
查看次数

多个连续的httpBackend请求会导致意外的请求

我正在测试轮询资源的序列,直到满足条件.

Book = $resource("/books/:id", {id: "@id"});

function poll(success) {
  Book.get({id:1}, function() {
    if (canStop) {
       success();
    } else {
       $timeout(poll, 1000);
    }

  });
};
Run Code Online (Sandbox Code Playgroud)

以下测试失败了 Error: Unsatisfied requests: GET /user_workshops/1

describe('poll', function() {
  beforeEach(function() {
    $httpBackend.expectGET('/books/1').respond(200,{id:1});
    $httpBackend.expectGET('/books/1').respond(200,{id:1, newVal:1});

    poll(function() {
       successCalled = true;
    });

    $httpBackend.flush();
    $timeout.flush();

    canStop=true;

    $httpBackend.flush();
  });

  it('should call success when canStop is true', function() {
     expect(successCalled).toBe(true);
  });
});
Run Code Online (Sandbox Code Playgroud)

我已经尝试重新排列测试顺序,将第二个放在第二个expectGET之前,httpBackend.flush()但后来我得到:

Error: Unexpected request: POST /books/1
No more request expected
Run Code Online (Sandbox Code Playgroud)

angular-resource httpbackend

5
推荐指数
1
解决办法
4344
查看次数

PUT请求通过URL发送params

我有一个看起来像这样的订单资源.

.factory('Order', order)

order.$inject = ['$resource', "ApiEndpoint"];

function order($resource, ApiEndpoint) {
  return $resource(ApiEndpoint.url + 'orders.json', {}, {
    create: {method: 'POST', url: ApiEndpoint.url + 'orders.json'},
    update: {method: 'PUT'},
    edit: {method: 'GET', url: ApiEndpoint.url + 'orders/edit.json'},
    remove_item: {method: 'GET', url: ApiEndpoint.url + 'orders/remove_item.json'},
  });
}
Run Code Online (Sandbox Code Playgroud)

我这样跑的Order.update时候

var params = {
  order: {
    line_items_attributes: {0: {quantity: 2, id: 1}}
  },
  order_id: 3
};

Order.update(params, function (resp, respHeaders) {
  console.log("response headers", respHeaders());
  console.log("change quantity resp", resp);
})
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

Order.update({}, params, function (resp, …
Run Code Online (Sandbox Code Playgroud)

http put params angularjs angular-resource

5
推荐指数
1
解决办法
1694
查看次数

为什么在资源查询转换中未定义数据变量

我很有棱角......所以我确信我做错了.花了好几个小时试图寻找一个解决方案,但angularjs文档旁边是无用的......几乎所有的例子都试图在全球范围内设置它.

我试图传递一个复杂的javascript对象作为查询参数.该对象有一个属性是一个数组,因此我需要展平(正确的术语??)这个对象,以便MVC绑定可以正确地实例化模型.

我试图传递的对象是沿袭的

newRequest = {
   SearchTerms: 'error',
   PageSize: 25,
   ...
   Facets: [
       { Field: 'createdBy', Value: 'Joe' },
       { Field: 'createdBy', Value: 'Mary' }
   ]
}
Run Code Online (Sandbox Code Playgroud)

我宣布我的资源如下

(function () {
    "use strict";

    angular
        .module('common.services')
        .factory('searchResource', ['$resource', 'appSettings', searchResource]);

    function searchResource($resource, appSettings) {
        return $resource(appSettings.searchPath, null, {
            query: {
                method: 'GET',
                transformRequest: function (data, headersGetter) {
                    if (data === undefined) {   // this is always true
                        return data;
                    }

                    return $.param(data);
                }
            }
        });
    }
}());
Run Code Online (Sandbox Code Playgroud)

我正在使用它

vm.executeSearch = function …
Run Code Online (Sandbox Code Playgroud)

angularjs angular-resource

5
推荐指数
1
解决办法
1318
查看次数

如何使用AngularJS,组件和ngResource在CRUD中实现C?

我是Angular的新手,我正在使用标准控制器和ngResource编写的简单CRUD应用程序来使用1.5中引入的组件.到目前为止,我发现的文档和资源都没有讨论如何:

  • 从头开始创建一个新项目
  • 与ngResource集成

所以我想知道是否有人可以就如何最好地进行提供一些指示.

我现有的应用程序有一个简单的工厂声明一个资源实体,一个控制器

  • 实例化资源的新实例: $scope.newEntity = new Entity();
  • $scope使用从后端检索的资源列表填充:Entity.query(function (data) { $scope.entities = data; });
  • 提供了一些用于删除,更新和保存资源到后端的功能.

在HTML中,我有一个使用的表单$scope.newEntity和控制器保存方法,以将新实体保存到后端.我还有一个ng-repeat列出存储的条目$scope.entities,还有几个额外的ng-clicks来执行一些编辑和删除.

我现在要做的是在列表中实现一些内联编辑.我知道我可以用我现有的方法做到这一点,但我想干净地重用我在实体编辑代码中的现有实体创建表单中的表单验证功能,而不重复.组件看起来非常适合我的(公认的无经验)眼睛.

与基于组件的方法,我按照在文档https://docs.angularjs.org/guide/component一个组件树的实施例,并创建了一个entity-listentity-detail组件.到目前为止,这些工作还可以,我想我可以弄清楚如何连接on-deleteon-update事件.我无法弄清楚的是如何处理一个on-create事件.

我应该使用一个完全独立的控制器和我现有的简单形式来处理创建事件吗?如果是这样,我如何让现有列表自动更新?该创建事件是否会传播到列表控制器?

或者我在现有的列表控制器中遗漏了什么?或者实体创建是细节控制器的特例?

我正在寻找有关如何使用Angular组件和ngResource实现此功能的信息,因为我还想为Angular 2做好准备.除非组件和资源不能一起使用,否则请不要发布有关如何使用完全不同的方法实现这一点,或者如何在没有组件的情况下重用HTML代码.谢谢!

angularjs angular-resource angularjs-components

5
推荐指数
1
解决办法
498
查看次数

使用$ resource时,ng-Table排序和搜索不起作用

我开始使用ngTable的现有示例,并使用ngResource对其进行了一些修改.使用资源工厂,它填充数据,但搜索和排序不起作用.

http://codepen.io/anon/pen/yVGKMZ

在上面创建了一支笔.

var app = angular.module("myApp", ["ngTable", "ngResource", "ngTableDemoFakeBackend"]);

app.factory('orderResource', function ($resource) {
    return $resource('https://jsonplaceholder.typicode.com/posts');
});

(function () {

    app.controller("demoController", demoController);
    demoController.$inject = ["NgTableParams", "$resource", "orderResource"];
    function demoController(NgTableParams, $resource, orderResource) {
        //debugger;
        var ordersGet = orderResource.query({ });


        var Api = $resource("/data");
        this.tableParams = new NgTableParams({}, {
            getData: function (params) {

                // **** Section 1 ***  sorting and filter does not work
                    return ordersGet.$promise.then(function (response) {
                    params.total(100);
                    return response;
              // **** Section 1 *** 
              
              
                //****Section 2 *** this works fine …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angular-resource ngtable

5
推荐指数
1
解决办法
1142
查看次数