In my small shopping angular app, i tried to send details of products that i added to cart and then i want to show details of products to another controller. I used service but when i debug it return empty array. I have two controllers: productlistcontroller.js:
app.controller("productListCtrl", function ($scope, $filter, productListActiveClass, productListPageCount, productListService) {
$scope.addProductToCart = function (product) {
$scope.cartArr = addProduct(product.ProductId, product.name, product.price);
productListService.save($scope.cartArr);
}
})
checkoutcontroller.js:
app.controller("cartSummaryController", function ($scope, productListService) {
$scope.cartData = productListService.getCartdata();
});
service.js
app.service("productListService", function ()
{
var cartArray = [];
this.save = function (cartArray) {
this.cartArray = cartArray;
}
this.getCartdata = function () {
return cartArray;
}
})
I cannot get cartArray in service . Thanks for your help.
return this.cartArray;this.cartArray = [];instead ofvar cartArray = [];in service