AngularJS POST and PUT not found - angularjs

Getting the list is not a problem but if I tried to Update or Add a record, it does not seem to work.
Here's the code:
angular.module('userApp.services',[])
.factory('User', function($resource){
return $resource('http://localhost/api/v1/users/:id',
{id:'#id'},
{
update: {
method: 'PUT'
}
});
});
My RESTful service is built in Laravel.

You are not injecting the ngResource module you need to use.
angular.module('userApp.services',['ngResource'])
.factory('User', ['$resource', function($resource){
return $resource('http://localhost/api/v1/users/:id', { id:'#id' }, {
update: { method: 'PUT' }
});
}]);

Related

Make an object for angularJS controller using $resource (factory|service)?

My approach is using AngularJS Service instead of factory along the project. Now I want to build an object called MyData which should be used as array in angularJS controller, such as:
vm.datas= MyData.query({}, function (datas) {
vm.thisData = {selected: datas[0].id};
});
As I searched in questions, I understood I can use factory as below:
angular.module('myDataService', ['ngResource']).
factory('MyData', function($resource){
return $resource(myURL, {}, {
query: {method:'GET', isArray:true}
});
});
What should I do if I want to use service. Is the code correct, below? What is the best practice?
angular
.module('app')
.service('myDataService', myDataService);
myDataService.$inject = ['ngResource'];
function myDataService($resource) {
var self = this;
self.MyData = $resource(myURL, {}, {
query: { method: 'GET', isArray: true }
});
}

ngResource search google news, but return results: Array[0]

sample code as follow,Is it about hl?locale? Many thanks.
.factory('newsResource', ['$resource', function($resource) {
return $resource('http://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=%E6%96%B0%E8%81%9E');
}])
newsResource.get(
function(successResponse){
console.log(successResponse);
}
);
You have to use JSONP when making a request to an external domain and it seems the googleapis also support the JSONP.
You could change your $resource to use JSONP instead like this:
.factory('newsResource', function($resource) {
return $resource('http://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=%E6%96%B0%E8%81%9E', {}, {
get: {
method: 'JSONP',
params: { callback: 'JSON_CALLBACK' }
}
});
});
Example Plunker: http://plnkr.co/edit/jCDqCErP9UKLWuhBOtp4?p=preview

AngularJS Resource - PUT method is not getting the id

I have a factory that returns the $resource for my Article model:
angular.module('ADI.Resources').factory("Articles", ['$resource', function($resource) {
return $resource('/api/v1/article/:articleId', {
articleId: '#_id',
_shop: window.user._shop
}, {
update: {
method: 'PUT'
}
});
}]);
GET, POST and DELETE are working well, but not the update (PUT). Here is the code I use:
Articles.update({articleId: '300000000000000000000001'}, function(article){
console.log(article);
});
It's making this request:
PUT http://localhost:3000/api/v1/article?_shop=100000000000000000000001
instead of:
PUT http://localhost:3000/api/v1/article/300000000000000000000001?_shop=100000000000000000000001
Any idea why the :articleId parameter is not filled when doing an update? Thanks!
As Fals mentionned in the comment, the articleId parameter was in the request content. So I made a little trick to have it also on the URI.
angular.module('ADI.Resources').factory("Articles", ['$resource', function($resource) {
return $resource('/api/v1/article/:articleId', {
articleId: '#_id',
_shop: window.user._shop
}, {
update: {
method: 'PUT',
params: {
articleId: "#articleId"
}
}
});
}]);
I had the same problem, the issue here was for the following line:
articleId: '#_id',
that line should be:
articleId: '#articleId',
articleId not need to be defined again.

angularjs integrate promise with resource

I have this resource:
myModule.factory('MyResource', ['$resource', 'geoLocationService', function ($resource, geoLocationService ) {
return $resource('/blabla', {}, {
'getData': { method: 'GET', params: { city: geoLocationService.getMyCity() } }
});
}]);
The problem is that by the time of calling MyResource.getData(), geoLocationService haven't done to fetch the location.
geoLocationService has a promise which will allow me call
geoLocationService.promise.then(...)
But I don't know how I can integrate this promise with the resource. Any idea?
EDIT
I am looking for something like:
myModule.factory('MyResource', ['$resource', 'geoLocationService', function ($resource, geoLocationService ) {
return $resource('/blabla', {}, {
'getData': { method: 'GET', beforeFetchPromise: geoLocationService.promise, { city: geoLocationService.getMyCity() } }
});
}]);
So only when geoLocationService.promise is resolved or rejected, the ajax call with parameters will occur.
I think you should add a promise in your controller:
$scope.resultsFromResource = YourResourceName.query().$promise.then(function(result){
// code inside promise
})
I would also suggest that you either use query as in my example or remove single quotes around your name getData if you want to keep that.

Services in AngularJS

I'm learning AngularJS and I've a little problem for services. The official tutorial gives an example for custom services with REST :
angular.module('phonecatServices', ['ngResource']).
factory('Phone', function($resource){
return $resource('phones/:phoneId.json', {}, {
query: {method:'GET', params:{phoneId:'phones'}, isArray:true}
});
});
So I made the same code with different names/url and it works.
I would like to pass a parameter to this service (an id in the url).
I tried to use $routeParams in it but it didn't work. Finally I found an other way to declare several function in the same service, so I made that :
factory('Article', function($resource) {
return {
getList: function(categoryId) {
return $resource('http://...', {
query: {method:'GET', isArray:true}
});
}
}
})
But it doesn't work for a REST call (with return 'Hello' for example, it's ok).
Do you know how do that ?
Thank you !
It sounds like you are not passing in the parameter into the query function.
angular.module('Article', ['ngResource']).
factory('Phone', function($resource){
return return $resource('http://.../:articleId', {
query: {method:'GET', isArray:true}
});
});
});
And then in your controller.
Article.query({
articleId : someVar
});

Resources