Search code examples
angularjsrestngresource

AngularJS ngResource not working


I have the following problem: I am using ui-router to display a ng-grid on a page. But the $resource is not changing the parameter from the url when sending to server (e.g. http://bcunix.test:8080/MYAPP/eq/:Id). The :Id is not changed, and I get Bad request from the server because I am sending :Id.

index.js

(function() {
'use strict';
var brgEquipments = angular.module('brg.equipments',
        [
            'ui.router',
            'brg.equipments.controllers'
        ]);
brgEquipments.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/equipments/list');

    $stateProvider.state('list', {
        url: '/equipments/list',
        templateUrl: '/scripts/modules/equipments/views/grid_list.html',
        controller: 'equipmentsListCtrl',
        resolve: {
            // A string value resolves to a service
            eqService: 'eqService',
            // A function value resolves to the return
            // value of the function
            equipments: function(eqService) {
                return eqService.query().$promise;
            }
        }
    });

});

})();

service.js

(function() {
'use strict';
var eqCtrls = angular.module('brg.equipments.controllers');

eqCtrls.factory('eqService', ['$resource', function($resource) {
        return $resource("http://bcunix.test:8080/MYAPP/eq/:Id",
                { Id: "@Id"},
        {
            update: {
                method: "PUT"
            },
            remove: {
                method: "DELETE"
            }
        });
    }]);

})();

Result: GET http://bcunix.test:8080/MYAPP/eq/:Id 400 (Bad Request)

Any help / hints would be appreciated.

Thanks, AdiP


Solution

  • Lol,

    So finally I figured it out. Looks like bower install ngResource is not equal to bower install angular-resource. I was using the wrong version of ngResource.

    Updated to latest version and now it's working.

    Thanks ThomasP1988 for your interest in solving my problem.

    Best regards, AdiP