Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm new to Angular and trying to figure out how to create a model to clean up my controller. I'm using Restangular and have created a factory that returns a model object.

I thought that I could do something like this...

model

Testimonials.factory('Testimonial', ['Restangular', function(Restangular) {

    /**
     * Constructor
     */
    function Testimonial() {
        // public properties, assigned to the instance ('this')
    }

    Testimonial.prototype.all = function() {
        return Restangular.all('testimonials').getList();
    }

    /**
     * Return the constructor function
     */
    return Testimonial;
}]);

controller

Testimonials.controller('TestimonialsController', ['$scope', 'Testimonial', function($scope, Testimonial) {

    Testimonial.all.then(function (testimonials) {
        $scope.testimonials = testimonials;
    });
}]);

I am receiving the error in chome dev tools, TypeError: Cannot read property 'then' of undefined.

How can I make this work? And is this a good way to implement a model?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

In your controller, you are missing the parentheses in your all method:

change:

 Testimonial.all.then(function (testimonials) {
        $scope.testimonials = testimonials;
 });

to:

 Testimonial.all().then(function (testimonials) {
        $scope.testimonials = testimonials;
 });

and you also need to return a new instance of Testimonial in your factory:

change:

return Testimonial;

to:

return new Testimonial();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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