Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 创建factory对象时出错_Angularjs_Ngresource - Fatal编程技术网

Angularjs 创建factory对象时出错

Angularjs 创建factory对象时出错,angularjs,ngresource,Angularjs,Ngresource,我有一个模块和控制器,其创建方式如下: var API_ENGINE_URL = 'localhost/angular/'; var mainApp = angular.module('mainApp', ['ngRoute', 'ngResource']); mainApp.controller('productController', ['$scope', 'ProductFactory', 'ProductListFactory','$routeParams', function ($

我有一个模块和控制器,其创建方式如下:

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;
    });
}]);
模型是这样创建的,文件被正确加载

 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}

    });
}]);
我在控制器中遇到如下错误。可能是什么错误。久违
在工厂函数中,返回值是Angular缓存和注入的值。没有必要自己实例化它

试试这个:

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;
    });
}]);

在工厂函数中,返回值是Angular缓存和注入的值。没有必要自己实例化它

试试这个:

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;
    });
}]);

订购时出现的问题应为:

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

订购时出现的问题应为:

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

这就是你要找的吗?这就是你要找的吗@Aman@pixelbits。有三个问题。1.使用
new
2。订购3。使用
$get
而不是
get
。谢谢你们两位@Aman@pixelbits。有三个问题。1.使用
new
2。订购3。使用
$get
而不是
get
。谢谢你们两位!