在角JS控制器之间传递数据? [英] Passing data between controllers in Angular JS?

查看:123
本文介绍了在角JS控制器之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示我的一个基本的控制器,

I have a basic controller that displays my products,

App.controller('ProductCtrl',function($scope,$productFactory){
     $productFactory.get().success(function(data){
           $scope.products = data;
     });
});

在我看来,我在列表中显示该产品

In my view I'm displaying this products in a list

<ul>
    <li ng-repeat="product as products">
        {{product.name}}
    </li>
</ul

我想要做的是当有人点击产品名称,我有一个名为另一种观点购物车,其中添加了此产品。

What I'm trying to do is when someone click on the product name, i have another view named cart where this product is added.

 <ul class="cart">
      <li>
          //click one added here
      </li>
      <li>
          //click two added here
      </li>
 </ul>

所以在这里我的疑问是:如何通过这个点击来自第一控制器产品,第二个?我以为,车应该是一个控制器也。

So my doubt here is, how do pass this clicked products from first controller to second? i assumed that cart should be a controller too.

我处理使用指令点击事件。另外我觉得我应该用服务来实现上述功能只是想不出如何?因为车将成为产品附加值predefined人数可能5/10取决于哪个页面用户。所以我想保持这种通用的。

I handle click event using directive. Also i feel i should be using service to achieve above functionality just can't figure how? because cart will be predefined number of products added could be 5/10 depending on which page user is. So i would like to keep this generic.

更新:

我创建了一个服务广播和在第二控制器我收到它。现在查询是如何更新DOM?由于我的名单砸产品是pretty硬codeD。

I created a service to broadcast and in the second controller i receive it. Now the query is how do i update dom? Since my list to drop product is pretty hardcoded.

推荐答案

从描述,好像你应该​​使用的服务。请查看 http://egghead.io/lessons/angularjs-sharing-data-between -controllers 和<一个href=\"http://stackoverflow.com/questions/17952620/angularjs-service-passing-data-between-controllers\">AngularJS服务传递控制器以看到一些例子之间的数据。

From the description, seems as though you should be using a service. Check out http://egghead.io/lessons/angularjs-sharing-data-between-controllers 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) {
      productList.push(newObj);
  };

  var getProducts = function(){
      return productList;
  };

  return {
    addProduct: addProduct,
    getProducts: getProducts
  };

});

依赖注入其服务于两个控制器。

Dependency inject the service into both controllers.

在您的ProductController的,定义一些动作所选对象添加到数组:

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

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

在您CartController,您可以通过服务产品:

In your CartController, get the products from the service:

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

这篇关于在角JS控制器之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆