0
votes

Please if anyone could help me,

I'm having a problem with ngResource, I've been searching for a solution for 2 hours but haven't found anything so I decided to put a question even though there are similar questions about this subject!

I'm using AngularJS version 1.4.7

I've been working with services just fine and then decided to use $resource,

the web service return a list of commercials.

here is app.js :

var myApp = angular.module('btAngApp' , ['ngMessages','ngResource'])
.run(function($rootScope){
    $rootScope.endPoint = "http://localhost:8082/serverprject/rest/";
});

the factory :

myApp.factory('commercialFactory', function($rootScope, $resource){
    return $resource($rootScope + 'commercial/findall');
});

the controller :

myApp.controller('editComCtrl', function($scope, $filter, $http, commercialFactory) {
$scope.init = function(){
    $scope.getAll();
};

$scope.getAll = function(){
    $scope.commercials = commercialFactory.query();
}

$scope.init();

I'm using $http and $filter in another code following the controller code above, I didn't copy it here.

I have included all the files above in index.html and downloaded angular-resource.min.js.

I've tried many scenarios but same error (make the controller and factory in the same file, have the code of the factory inside of the controller, ....) if I don't inject ngResource in angular.module I get this error :

Error: [$injector:unpr] http://errors.angularjs.org/1.4.7/$injector/unpr?p0=%24resourceProvider%20%3C-%20%24resource%20%3C-%20editComCtrl

If I do I get this :

Error: [$injector:modulerr] http://errors.angularjs.org/1.4.7/$injector/modulerr?p0=btAngApp&p1=%5B%24inj....

2

2 Answers

1
votes

the factory :

return $resource($rootScope + 'commercial/findall');

maybe this should be: $rootScope.endPoint + 'commercial/findall'


For the provider error, instead, you should ensure that ngResource will be loaded before your app, and, of course, after angularjs

1
votes

In the factory you are returning the resource object directly.

In the factory you should have some methods or variable which you can access it using the factory

I guess you should assign it to some variable and then return so that you can access it using the factory.

something like this..

 return {
            resourceHandle: $resource($rootScope + 'commercial/findall')
    }

and then use it in the controller as

$scope.getAll = function(){

    $scope.commercials = commercialFactory.resourceHandle.query();
}

$scope.init();