Creating a shopping cart app I'm trying to pass an object into a service function using Angular. I took the advice from another post but for some reason I'm still getting an unprovided error on page load, and a syntax error for some odd reason. The errors seem to stem from here:
<form class="cart nobottommargin clearfix" ng-submit="addToCart({{producto}})" novalidate >
The controller:
.controller('DetalleProductoController',
['$scope', '$stateParams', 'Productos', 'Carrito',
function ($scope, $stateParams, Productos, Carrito) {
// Recibir el producto por id pasado en el UI-router
var prod = Productos.findById($stateParams.id);
$scope.producto = prod;
//ng-click addToCart desde el factory
$scope.addToCart = Carrito.addToCart(prod);
}])
And the service it implements (Shortened):
MyApp.service('Carrito', ['Productos', '$scope', '$http',
function (Productos, $scope, $http){
var carritoUsuario = [];
var todosLosProductos = Productos.todos();
this.addToCart = function(Prod) {};
Any help would be very much appreciated...
edit: Entire service here. Maybe there's something I'm fundamentally doing wrong?
MyApp.service('Carrito', ['Productos', '$scope', '$http',
function (Productos, $scope, $http){
var carritoUsuario = [];
var todosLosProductos = Productos.todos();
var unProducto = function() {
this.Nombre = "";
this.stockid = 0;
this.Descripcion = "";
this.Precio = 0;
this.Familia = 1;
this.Modificadores = [];
this.Cantidad = 1;
this.SKU = function () {return "" + this.stockid +
this.Modificadores.mod1 + this.Modificadores.mod2 +
this.Modificadores.mod3 + this.Modificadores.mod4 +
this.Modificadores.mod5};
}
/* For now this searches the local array */
this.findBySKU = function(sku) {
console.log("Finding: " + sku + " from Carrito service findBySKU...");
_.find(carritoUsuario, function (producto){
return producto.stockid == sku;
});
};
this.getProducto = function(id) {
console.log("Getting: " + id + " from Carrito service getProducto...");
Producto.findById(id);
};
this.getCart = function() {
console.log("Getting cart from Carrito service getCart");
carritoUsuario;
};
this.addToCart = function(Prod) {
/* Prod es un ng-model pasasdo por ng-click=addtoCart(producto)
en cualquier boton de compra. Hay que cargar los datos en el html */
console.log("Passed: " + Prod + " -- into Carrito.addToCart...");
unProducto.Nombre = Prod.Nombre;
unProducto.stockid = Prod.stockid;
unProducto.Descripcion = Prod.Descripcion;
unProducto.Precio = Prod.Precio;
unProducto.Familia = Prod.Familia;
unProducto.Cantidad = parseInt(Prod.cantidad);
unProducto.Modificadores = {
"mod1":Prod.mod1,
"mod2":Prod.mod2,
"mod3":Prod.mod3,
"mod4":Prod.mod4,
"mod5":Prod.mod5
};
unProducto.SKU = "" + Prod.stockid + Prod.mod1 + Prod.mod2 + Prod.mod3 + Prod.mod4 + Prod.mod5;
if(angular.isDefined(Carrito.findBySKU(unProducto.SKU))) {
var i = _.findIndex(this.carritoUsuario, unProducto.SKU);
Carrito.carritoUsuario[i].Cantidad++;
} else {
Carrito.carritoUsuario.push(unProducto);
console.log("Added: " + unProducto + " -- to the Carrito...");
}
};
this.totalCarrito = function() {
var total = 0;
_.forEach(carritoUsuario, function (e) {
total += parseFloat(e.Precio) * parseFloat(e.Cantidad);
});
return total;
}
}]);
Rewritten as a factory, still the same unprovided error:
MyApp.factory('Carrito', ['Productos', '$scope', '$http',
function (Productos, $scope, $http){
var carritoUsuario = [];
var todosLosProductos = Productos.todos();
var unProducto = function() {
this.Nombre = "";
this.stockid = 0;
this.Descripcion = "";
this.Precio = 0;
this.Familia = 1;
this.Modificadores = [];
this.Cantidad = 1;
this.SKU = function () {return "" + this.stockid +
this.Modificadores.mod1 + this.Modificadores.mod2 +
this.Modificadores.mod3 + this.Modificadores.mod4 +
this.Modificadores.mod5};
}
/* For now this searches the local array */
return {
findBySKU : function(sku) {
console.log("Finding: " + sku + " from Carrito service findBySKU...");
return _.find(carritoUsuario, function (producto){
return producto.stockid == sku;
});
},
getProducto : function(id) {
console.log("Getting: " + id + " from Carrito service getProducto...");
return Producto.findById(id);
},
getCart : function() {
console.log("Getting cart from Carrito service getCart");
return carritoUsuario;
},
addToCart : function(Prod) {
/* Prod es un ng-model pasasdo por ng-click=addtoCart(producto)
en cualquier boton de compra. Hay que cargar los datos en el html */
console.log("Passed: " + Prod + " -- into Carrito.addToCart...");
unProducto.Nombre = Prod.Nombre;
unProducto.stockid = Prod.stockid;
unProducto.Descripcion = Prod.Descripcion;
unProducto.Precio = Prod.Precio;
unProducto.Familia = Prod.Familia;
unProducto.Cantidad = parseInt(Prod.cantidad);
unProducto.Modificadores = {
"mod1":Prod.mod1,
"mod2":Prod.mod2,
"mod3":Prod.mod3,
"mod4":Prod.mod4,
"mod5":Prod.mod5
};
unProducto.SKU = "" + Prod.stockid + Prod.mod1 + Prod.mod2 + Prod.mod3 + Prod.mod4 + Prod.mod5;
if(angular.isDefined(Carrito.findBySKU(unProducto.SKU))) {
var i = _.findIndex(this.carritoUsuario, unProducto.SKU);
Carrito.carritoUsuario[i].Cantidad++;
} else {
Carrito.carritoUsuario.push(unProducto);
console.log("Added: " + unProducto + " -- to the Carrito...");
}
},
totalCarrito : function() {
var total = 0;
_.forEach(carritoUsuario, function (e) {
total += parseFloat(e.Precio) * parseFloat(e.Cantidad);
});
return total;
}
};
}]);