lukassz lukassz - 14 days ago 8
AngularJS Question

AngularJS function in factory call to query from $resource

I write simple factory which call to my rest endpont, but I would like use different address not only one, but when I execute

query
from
$resource
I get only text function in my console log.

This is my factory:

angular.module('blogfontApp.services', []).factory('Blog', function ($resource) {

return {
getAll : function () {
return $resource('http://localhost:8080/api/blog');
}
}

});


and my controller:

angular.module('blogfontApp.controllers', []).controller('BlogListController',

function ($scope, Blog ) {

$scope.bloglist = Blog.getAll.query();


console.log($scope.bloglist);

});


How to call to different address from my factory?

Answer

Try below using promise-

iApp.controller('TestController', function($scope, Blog) {
  Blog.getAll().query().$promise.then(function(result) {
    //success
    $scope.bloglist = result;
    console.log($scope.bloglist);
  }, function(errResponse) {
    // fail
  });
});
Comments