notnotundefined
notnotundefined

Reputation: 3751

Getting an error while creating factory object

I have a module and controller which are created like :

var API_ENGINE_URL = 'localhost/angular/';
var mainApp = angular.module('mainApp', ['ngRoute', 'ngResource']);


mainApp.controller('productController', ['$scope', 'ProductFactory', 'ProductListFactory','$routeParams', function ($scope, ProductFactory,routeParams, ProductListFactory) {
    var productList = new ProductListFactory();// THROWS ERROR HERE
    var promise = productList.$get({id: $routeParams.category_id}).$promise;
    promise.then(function (productList) {
        $scope.productList = productList;
    });
}]);

and the Model is created like this, files are properly loaded

 mainApp.factory('ProductListFactory', ['$resource',function ($resource) {
    return $resource(API_ENGINE_URL + 'category/:id/items', {}, {
        get: {method: 'GET', params: {category_id: '@category_id', id: '@id'},isArray:true,url:API_ENGINE_URL+'product?store_id=:storeId'},
        save: {method: 'GET', isArray: true}

    });
}]);

I am getting an error in the controller like below. what could be the error. Stuck for a long time enter image description here

Upvotes: 0

Views: 34

Answers (2)

Aman Gupta
Aman Gupta

Reputation: 3797

Problem is in ordering should be :

['$scope', 'ProductFactory', 'ProductListFactory','$routeParams', function ($scope, ProductFactory,ProductListFactory,$routeParams) 

Upvotes: 1

Michael Kang
Michael Kang

Reputation: 52847

In a factory function, the return value is what's cached and injected by Angular. There is no need to instantiate it yourself.

Try this:

mainApp.controller('productController', ['$scope', 'ProductFactory', 'ProductListFactory','$routeParams', function ($scope, ProductFactory,routeParams, ProductListFactory) {
    var promise = ProductListFactory.$get({id: $routeParams.category_id}).$promise;
    promise.then(function (productList) {
        $scope.productList = productList;
    });
}]);

Upvotes: 1

Related Questions