RESTful services with $resource

#1

I have a working angularjs application that uses angular-resource.js and $resource to access RESTful services.

I now want to move this to ionic but

angular-resource.js is in the myApp/lib/ionic/js/angular/ folder

but when I try and do something like this

.controller(‘AccountCtrl’, function($scope, $resource){
$scope.login = $resource(‘http://fv-host:9381/fv/User/:action’,
{action:‘login’,
id:’@id’,
password:’@password’,
remember:‘false’,
callback:‘JSON_CALLBACK’},
{get:{method:‘POST’}}
);

It blows and reports

Error: [$injector:unpr] Unknown provider: $resourceProvider <- $resource

I have seen a number of posts about the browser test not loading cordova.js so do I assume that its not loading angular-resource.js either.

I didnt really follow the argument that ionic being a hybrid dev platform meant that not loading js libs in the browser was a good thing, seems to me impossible to test if its not loading the cordova and angular js files.

Any help really appreciated as without $resource my app is dead in the water ( or at least screaming in a vacuum)

#2

I’ve always formatted my controllers like this so that the variables added via dependency injection are provided to the controller via an array. Perhaps this could explain why there is a missing dependency in your app?

.controller('AccountCtrl', ['$scope', '$resource', function($scope, $resource){
...
}]);

Otherwise, make sure you include a reference to this from your index.html:

<script src="lib/ionic/js/angular/angular-resource.js"></script>
#3

thanks seems like there was some odd dependency a clean install and it works ish

#4

hi, @duuuudeman , i same the problem. but i create services.js (starter.services as module name) for instance ng-resource($resource) but error? sometihing wrong?
i have add depedency in App.js ['starter.services'] and injected in controller. but still error.

.controller('SectionCtrl', ['IonicServices', '$scope',
	function(IonicServices, $scope){
		IonicServices.query(function(results){
			$scope.sections = results;
		},
		function(err){
			console.log(err);
		});
}])

and this is my services:

angular.module('starter.services', [])
	.factory('IonicServices', ['$resource',
		function($resource){
			return $resource('http://api.situssunnah.com/api/sections.json');
	}]);

error message:

Error: [$injector:unpr] Unknown provider: $resourceProvider <- $resource <- IonicServices

i have define in index html and define angular-resource.js.
thank you, sorry for bad my english grammar

#5

I got it to work easily in my browser. But when I deploy the app to my phone, the data can’t be written (I’m trying to read a local json file from the JS folder)

Any hints why that could be the case?

thanks!

#6

Some of the dependencies are not included in default ionic bundle. So you want to use that services, need to include it manually on your index.html page below the ionic bundle js.

<script src="lib/ionic/js/angular/angular-resource.min.js"></script>

And add to your service ngResource as dependency injection on your app likes:

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

Finally, inject your service $resource to your controller as a dependency. That’s all.

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