Passing data between controllers in Angular JS?

kishanio提出了一个问题:Passing data between controllers in Angular JS?,或许与您遇到的问题类似。


From the description, seems as though you should be using a service. Check out and AngularJS Service Passing Data Between Controllers to see some examples.

You could define your product service as such:

app.service('productService', function() {
  var productList = [];

  var addProduct = function(newObj) {

  var getProducts = function(){
      return productList;

  return {
    addProduct: addProduct,
    getProducts: getProducts


Dependency inject the service into both controllers.

In your ProductController, define some action that adds the selected object to the array:

app.controller('ProductController', function($scope, productService) {
    $scope.callToAddToProductList = function(currObj){

In your CartController, get the products from the service:

app.controller('CartController', function($scope, productService) {
    $scope.products = productService.getProducts();