javascript - how to access value object from returned service in angular? - Stack Overflow

I have a service which returns an actual data. but when I call it in the controller, the return data st

I have a service which returns an actual data. but when I call it in the controller, the return data structure is a bit confusing.

here is my factory:

app.factory("studentService", ['$http','$q','$localstorage','$state', 
    function($http,$q,$localstorage,$state) {

 return{
      getStudentsFrom : function(index){
                 return $http.get(app.baseUrlServer + app.getStudentsUrl + '/getbasedonindex/'+index)
                    .then(function(response){
                        if (response.data) {
                            return response.data;
                        } else {
                            // invalid response
                            return $q.reject(response.data);
                        }
                    },function(response){
                        // invalid response
                        return $q.reject(response.data);
                    });
            }
 }

}]);

I have a service which returns an actual data. but when I call it in the controller, the return data structure is a bit confusing.

here is my factory:

app.factory("studentService", ['$http','$q','$localstorage','$state', 
    function($http,$q,$localstorage,$state) {

 return{
      getStudentsFrom : function(index){
                 return $http.get(app.baseUrlServer + app.getStudentsUrl + '/getbasedonindex/'+index)
                    .then(function(response){
                        if (response.data) {
                            return response.data;
                        } else {
                            // invalid response
                            return $q.reject(response.data);
                        }
                    },function(response){
                        // invalid response
                        return $q.reject(response.data);
                    });
            }
 }

}]);

Here is my controller that calls it

var app_ctrl = angular.module('app.controllers');

app_ctrl.controller('BrowseController',['$scope','$state', 'studentService', '$localstorage', 
	function($scope,$state, studentService, $localstorage, $timeout) {
  
   $scope.noMoreItemsAvailable = false;
 // $scope.NumOfRows=studentService.getNumOfRows();

  $scope.students = [];
  $scope.counter=1;

console.log(studentService.getStudentsFrom($scope.counter));

  

}]);

and here is what I get.

Promise {$$state: Object, then: function, catch: function, finally: function}
$$state: Object
status: 1
value: Array[1]
0: Object
length: 1
__proto__: Array[0]
__proto__: Object
__proto__: Object

The question is, how can I access the value?, thanks

Share Improve this question asked May 23, 2015 at 5:53 Bilal BayasutBilal Bayasut 1213 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

The function $http.get return a promise you need to use it on this way :

studentService.getStudentsFrom($scope.counter).then(
    function(result) {
       console.log(result);
    }
);

see https://docs.angularjs/api/ng/service/$http or https://docs.angularjs/api/ng/service/$q

before you explore the $http promises and async calls further, i think you may have to fix this: from var app_ctrl = angular.module('app.controllers');
to this var app_ctrl = angular.module('app.controllers', []);

angularjs docs: "The empty array in angular.module('myApp', []). This array is the list of modules myApp depends on."

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745560379a4633073.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信