【问题标题】:Understanding the $resource factory and the @ prefix了解 $resource 工厂和 @ 前缀
【发布时间】:2014-07-30 18:39:50
【问题描述】:

鉴于以下服务:

vdgServices.factory('UserService', ['$resource',
function($resource) {

    return $resource('api/users/:id', {}, {

        doGet: {
            method: 'GET',
            params: { id: '@userId' }
        },

        doPost: {
            method: 'POST',
            params: { id: '@userId' }
        },

        doPut: {
            method: 'PUT',
            params: { id: '@userId' }
        },

        doDelete: {
            method: 'DELETE',
            params: { id: '@userId' }
        }

    });

}]);

我观察到以下请求的 URL:

var params = { userId: 42 };
var onSuccess = function() { console.log("OK"); };
var onError = function() { console.log("KO"); };

UserService.doGet(params, onSuccess, onError);
// requests api/users?userId=42

UserService.doPost(params, onSuccess, onError);
// requests api/users/42

UserService.doPut(params, onSuccess, onError);
// requests api/users/42

UserService.doDelete(params, onSuccess, onError);
// requests api/users?userId=42

谁能解释为什么:id URL 参数有时会被42 替换,有时不会?

理想情况下,我希望将其替换为任何方法,即每次请求的 URL 都变为“api/users/42”。

【问题讨论】:

    标签: javascript angularjs rest url-parameters ngresource


    【解决方案1】:

    AngularJS $资源

    如果参数值以 @ 为前缀,则该参数的值将从数据对象上的相应键中获取(对于非 GET 操作很有用)。

    你把参数放错地方了,你应该这样实现

    .factory('UserService', function($resource) {
        return $resource('api/users/:id', { id: '@id' }, {
    
            doGet: {
                method: 'GET'
            },
    
            doPost: {
                method: 'POST'
            },
    
            doPut: {
                method: 'PUT'
            },
    
            doDelete: {
                method: 'DELETE'
            }
    
        });
    });
    

    让我们测试一下

    describe('userApp', function () {
        var UserService
          , $httpBackend
        ;
    
        beforeEach(function () {
            module('userApp');
        });
    
        beforeEach(inject(function (_UserService_, _$httpBackend_) {
            UserService = _UserService_;
            $httpBackend = _$httpBackend_;
        }));
    
        describe('User resource - api/users', function () {
            it('Calls GET – api/users/{id}', function() {
                $httpBackend.expectGET('api/users/42').respond(200);
    
                UserService.doGet({id: 42});
    
                $httpBackend.flush();
            });
    
            it('Calls POST - api/users/{id}', function() {
                $httpBackend.expectPOST('api/users/42').respond(200);
    
                UserService.doPost({id: 42});
    
                $httpBackend.flush();
            });
    
            it('Calls PUT - api/users/{id}', function() {
                $httpBackend.expectPUT('api/users/42').respond(200);
    
                UserService.doPut({id: 42});
    
                $httpBackend.flush();
            });
    
            it('Calls DELETE - api/users/{id}', function() {
                $httpBackend.expectDELETE('api/users/42').respond(200);
    
                UserService.doDelete({id: 42});
    
                $httpBackend.flush();
            });
        });
    });
    

    jsfiddle:http://jsfiddle.net/krzysztof_safjanowski/vbAtL/

    【讨论】:

      猜你喜欢
      • 2011-05-07
      • 1970-01-01
      • 2011-10-17
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 2015-03-04
      • 1970-01-01
      相关资源
      最近更新 更多