저는 AngularJS에서 초보자입니다. Adam Freeman의 Pro AngularJS 책에서 예제를하고 있지만이 오류로 인해 막혔습니다.
Error: [$injector:pget] Provider 'function()' must define $get factory method.
나는 카트 위젯과 요약 부분보기를 만들어야하는 시점에 있습니다. 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;
}
}
}
});
이 을 시도
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;
});
});
모듈 sportsstore에 대한 코드를 게시 할 수 있습니까? – sma
sportsStore 모듈 코드가 추가되었습니다. – Pak
나는 어리석은 실수를 저 지르고있었습니다. 나는 함수 호출 다음의 첫 번째 인수를 놓쳤다. .factory ("cart", function(). 그러나 질문을 게시 할 때 응용 프로그램의 잘못된 위치에서 가져 왔습니다. 정말 죄송합니다. 게시 된 코드를 업데이트하여 정확한 문제. – Pak