-1
angular.module('app').factory('mvsomeManager', 
 function ($q, $http, mvSomething){
    return{
        getsomedata:function(){
            var data = mvSomething.query();
            if(response){
              dfd.resolve(response);
            }else{
                dfd.reject({reason:"error"});
            }
            return dfd.promise;//No data is returned
    };
 });
//usage
angular.module('app').controller('mvsomelCtrl', function ($scope, mvsomeManager,mvSomething){
    //same thing in factory but now it returns list without problem
    $scope.querydata = mvSomething.query();//This works
    $scope.querydata = mvsomeManager.getsomedata();//This does not works!
 });

Can you not call query on a factories? I am able to call Delete, put, etc on Factories using the $save,$update. but just cant use GET (query) to return a list of data. Only works on controller.

Nix
  • 57,072
  • 29
  • 149
  • 198
user516883
  • 8,868
  • 23
  • 72
  • 114
  • 1
    You do have extra code in there and you are not returning the resulting data. If you want more help you need to provide working code or explain what `dfd` and `response` are as you do not define them in your code sample (*downvote was not me*). – Igor Feb 10 '16 at 16:28
  • `getsomedata` returns a promise. Once its resolved you will have your data, whatever `response` is. Use `.then/.catch` etc to handle it. Look at angulars [promises](https://docs.angularjs.org/api/ng/service/$q). Pretty much the same syntactically to ES6. – ste2425 Feb 10 '16 at 17:12

1 Answers1

1

You have one operation that is synchronous (query) and the other (getsomedata) is deferred. So you need to use .then() to access the data.

//this call is synchonous
$scope.querydata = mvSomething.query();//This works
//this call is async 
mvsomeManager
       .getsomedata()
       .then(onGetDataSuccess, onGetDataError);

function onGetDataError(error){
    alert('error, please check console logs');
    console.error(error);
}

function onGetDataSuccess(data){
    $scope.querydata = data;
}
Nix
  • 57,072
  • 29
  • 149
  • 198