1

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.

2
  • 1
    I think it should be return this.cartArray; Commented Dec 19, 2016 at 10:48
  • 2
    use this.cartArray = []; instead of var cartArray = []; in service Commented Dec 19, 2016 at 10:51

2 Answers 2

1

The problem is that you call to getCartdata method only once the controller is loaded and not when the data is changed. Instead, pass a pointer to the service function to your controller

Replace:

$scope.cartData = productListService.getCartdata();

In:

$scope.cartData = productListService.getCartdata;

And in your view you can do something like:

<div ng-repeat="item in cartData()">{{item}}</div>

This way you will get updated data.

Sign up to request clarification or add additional context in comments.

Comments

1

In addition to the issue Satpal described there is an other issue in the Code: you are asking for 'productListService.getCartdata()' at the creation of the cartSummaryController. But the actual value will be set later (maybe based on user input).

So you either need to add some publishing mechanics to your service, so it notifies others / subscribers of changes to the data. Or you can return an object from .getCartdata(). And when you get the data from save you populate the object you already returned in the past.

EDIT: added slightly modified code to showcase how it could work:

var app = angular.module("app",[]);

app.controller("productListCtrl", function ($scope, productListService) {
  $scope.addProductToCart = function (product) {
    productListService.save($scope.data);
  }
});

app.controller("cartSummaryController", function ($scope, productListService) {
    $scope.container = productListService.getCartdata();
});

app.service("productListService", function ()
{  
    var container = {cartData: []};
    this.save = function (name) {
        container.cartData.push(name);
    };
    
    this.getCartdata = function () {
        return container;
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
  <div ng-controller="productListCtrl">
    Product Controller - type text and click 'addToCart':
    <br/>
    <input type="text" ng-model="data" />
    <input type="button" value="addToCart" ng-click="addProductToCart()">
  </div>
  <div ng-controller="cartSummaryController">
      CartData - updates automatically:
    <ul>
      <li ng-repeat="entry in container.cartData">
        {{entry}}
      </li>
    </ul>
  </div>
</body>

This way you can bind container.cartArray to the html inside your requesting controller, and it will be updated whenever save is called.

3 Comments

the first time service was called in productlistcontroller i can get cartArray and then before it move to controller it call again in service so that it run cartArray = [] again , therefore in cartSummaryController it will get empty array of cartArray
not sure if you refer to your original code or mine, and i did not really understood what you mean with 'move' to controller and 'call again'. You have two flaws in your code 1. when you save you assign the cartData to this and when you get the data you read the empty cartData from the controllers function scope (not $scope!), and therefore you will allways get an empty array. 2. (not sure if it's a real issue or it is just because you cut out some of the code) your cartSummaryController calls get only once at startup.
i modified my example and created a working code example, which is slightly different to your original code (to make it short), but you should get the idea. - Just hit run and you can see that and how it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.