AngularJS - 错误:[$ injector:pget] Provider'function()'必须定义$ get factory方法

时间:2014-10-09 19:15:26

标签: javascript angularjs get provider

我是AngularJS的新手。我正在从Adam Freeman的Pro AngularJS书中做一个例子,但是却遇到了这个错误。

Error: [$injector:pget] Provider 'function ()' must define $get factory method. 

我正处于创建Cart Widget和摘要局部视图的位置。当AngularJS尝试加载Cart模块时,抛出上述错误。

以下是Chrome开发者工具的部分错误消息。作者此时未提及有关$ get函数或提供程序的任何内容。不知道我做错了什么。请帮忙。

Uncaught Error: [$injector:modulerr] Failed to instantiate module sportsStore due to:
Error: [$injector:modulerr] Failed to instantiate module cart due to:
Error: [$injector:pget] Provider 'function ()' must define $get factory method.
http://errors.angularjs.org/1.2.23/$injector/pget?p0=function%20()

购物车小部件代码。

angular.module("cart", [])
.factory( function() {

    var cartData = [];

    return {

    addProduct: function (id, name, price) {
        var addedToExistingItem = false;
        for (var i = 0; i < cartData.length; i++) {
            if (cartData[i].id == id) {
                cartData[i].count++;
                addedToExistingItem = true;
                break;
            }
        }
        if (!addedToExistingItem) {
            cartData.push({
                count: 1, id: id, price: price, name: name
            });
        }
    },

    removeProduct: function (id) {
        for (var i = 0; i < cartData.length; i++) {
            if (cartData[i].id == id) {
                cartData.splice(i, 1);
                break;
            }
        }
    },

        getProducts: function () {
            return cartData;
        }
    }
})
.directive("cartSummary", function (cart) {
    return {
        restrict: "E",
        templateUrl: "components/cart/cartSummary.html",
        controller: function ($scope) {

            var cartData = cart.getProducts();

            $scope.total = function () {
                var total = 0;
                for (var i = 0; i < cartData.length; i++) {
                    total += (cartData[i].price * cartData[i].count);
                }
                return total;
            }

            $scope.itemCount = function () {
                var total = 0;
                for (var i = 0; i < cartData.length; i++) {
                    total += cartData[i].count;
                }
                return total;
            }
        }
    }
});

这是从app.html调用它的方式

<script>
    angular.module("sportsStore", ["customFilters", "cart"]);   
</script>
<script src="controllers/sportsStore.js"></script>
<script src="filters/customFilters.js"></script>
<script src="controllers/productListControllers.js"></script>
<script src="components/cart/cart.js"></script>

...

<body ng-controller="sportsStoreCtrl">
    <div class="navbar navbar-inverse">
        <a class="navbar-brand" href="#">SPORTS STORE</a>
        <cart-summary />
    </div>
    <div class="alert alert-danger" ng-show="data.error">
        Error ({{data.error.status}}). The product was not loaded.
        <a href="/sportsstore/app.html" class="alert-link">Click here to try again</a>
    </div>
    <ng-include src="'views/productList.html'" />
</body>

SportsStore模块代码..

angular.module("sportsStore")
    .constant("dataUrl", "http://localhost:5500/products")
    .controller("sportsStoreCtrl", function ($scope, $http, dataUrl) {

        $scope.data = {}
        $http.get(dataUrl)
            .success(function (data) {
                $scope.data.products = data;
            })
            .error( function (error) { 
                $scope.data.error = error;
            });
 });

3 个答案:

答案 0 :(得分:1)

您是否在某处将模块定义为“购物车”,当您尝试定义工厂时,看起来可能存在引用模块中“cart”的问题。如果您尝试引用主应用程序,请尝试以下操作:

var main = angular.module("sportsStore", ['cart'])...

然后定义您的购物车工厂尝试:

main.factory('cart', function() {... cart factory code here...})

答案 1 :(得分:0)

试试这个 删除模块[]   而你的模块名称不是购物车 购物车改为sportsStore

     angular.module("sportsStore")
.factory("cart", function() {     ... })

答案 2 :(得分:0)

尝试使用您的cart.js文件代码,因为它与本书中的代码不同:

angular.module("cart", [])    
.factory("cart", function () {...}