I would like to get array object from server (java). Below there is angular method :
service.getAthors = function(){
var deferred = $q.defer();
var authors = authorResource.query(function() {
console.log(authors);
}).$promise.then( function(){
deferred.resolve( "Adding book have gone correctly." );
}, function(){
deferred.reject("Error during adding new book.");
});
}
In console firebug's i see this: [{"author_id":7,"author":"Dan Brown"}]
but authors array is empty. Can you tell me why ?
You need to assign authors
to the returned server data.
authorResource.query(function() {
console.log(authors);
}).$promise.then( function(data){
authors = data;
deferred.resolve( "Adding book have gone correctly." );
}, function(){
deferred.reject("Error during adding new book.");
});
{ 'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'} };
authorResource.query({isArray:true},function() {
console.log(authors);
}).$promise.then( function(data){
authors = data;
deferred.resolve( "Adding book have gone correctly." );
}, function(){
deferred.reject("Error during adding new book.");
});
you are using query method use isArray:true
see the reference https://docs.angularjs.org/api/ngResource/service/ $resource
Angular $resource has a little inconsistence when an action with isArray:true exist.
If you call the resource class and not the instance, works of different ways, example:
myModule.controller('controller',function(MyResource){
//In this MyResource is the class
var myElement = MyResource.get({id:1}); //myElement is an instance
var array = MyResource.query(); //When the query response the array gonna be filled
var arrayFromMyElement = myElement.query() // Return a promise not a array and the data of the promise gonna get the array
arrayFromMyElement.then(function(data){
//Data is the array
})
});
This is observable in the $resource source code:
https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js#L627-L638
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.