0

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

notnotundefined
  • 3,493
  • 3
  • 30
  • 39

2 Answers2

1

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;
    });
}]);
Michael Kang
  • 52,003
  • 16
  • 103
  • 135
  • @Aman @pixelbits . there are 3 problems . 1. usage of `new` 2. Ordering 3. using `.$get` instead of `get` . Thanks a lot both of you! – notnotundefined Jan 29 '16 at 13:05
1

Problem is in ordering should be :

['$scope', 'ProductFactory', 'ProductListFactory','$routeParams', function ($scope, ProductFactory,ProductListFactory,$routeParams) 
Aman Gupta
  • 3,627
  • 4
  • 40
  • 64