6

I try to use .then() in my angular controller

 angular.module('n2goApp')
        .controller('MainCtrl', function($scope, Products) {        
                Products.get(). then( function ( response ) {
                    $scope.data = response;
                    console.log(response);
                    $scope.totalPages = response.TotalPages;
                    $scope.totalItems = response.total;
                    $scope.products = response.data;
                    $scope.currentPage = response.current_page;
                    $scope.maxSize = 5;
                    $scope.setPage = function(pageNo) {
                        $scope.currentPage = pageNo;
                    };
                });
});

but drops me an error

Error: Products.get(...).then is not a function

Service

angular.module('n2goApp')
  .service('N2goservice', function N2goservice() {
    // AngularJS will instantiate a singleton by calling "new" on this function
  }).factory('Products', ['$resource', function($resource) {

        var url = 'http://domain.com/api/products';
    return $resource( url + '/:prodID', 
        { bookId: '@prodID' }, { 
            loan: { 
                method: 'PUT', 
                params: { prodId: '@prodID' }, 
                isArray: false 
            },
                        get:{
                             method: 'GET', 
                 params: { 
                                 prodId: '@prodID',
                                 page:'@page'
                                }, 
                isArray: false
                        }
            /* , method3: { ... } */
        } );

}]);

What I'm doing wrong?

||||||
  • Can you provide the code of the Products service? – cuttlas Jun 28 '14 at 13:26
  • added service to post – fefe Jun 28 '14 at 13:31
19

You have to get the $promise from the resource. Like this:

Products.get().$promise.then(..)
||||||
7

Or you can use the other syntax with callback

Products.get({},function(response) {
});
||||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.