在角跨控制器共享分类汇总 [英] Sharing subtotal across controllers in Angular

查看:117
本文介绍了在角跨控制器共享分类汇总的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分享一个控制器与另一个控制器计算的分类汇总。当我使用code以下,我收到了总没有定义错误。

我真的AP preciate如何得到这个建议的工作权利。

我使用 的这个职位#1作为一种模式。

  .factory('serviceA',函数(){
   VAR serviceA = {
   金额:空
  };
   返回serviceA;
}).controller('ProjectListCtrl',函数($范围,serviceA,项目){
  VAR projectList =这一点;
  projectList.projects =项目;
  serviceA.amount =总;   projectList.total =功能(){   无功总= 0;   angular.forEach(projectList.projects,功能(项目){
    总+ = project.type.cost;    });
   总回报; //想用这个总在其他控制器
 };
}).controller('PaymentFormCtrl',
  功能($范围,$ HTTP,serviceA){
  $ scope.serviceA = serviceA;
  的console.log(serviceA);
//。 。 。
});


解决方案

您可能想在这种情况下服务是单身,这意味着只有一个会被创建,每个控制器将使用相同​​的服务来使用服务,而不是他们使用自己的工厂。

 。服务('serviceA',函数(){
    无功量= 0;
    返回{
        得到:函数(){
            返回金额;
        },
        设置:功能(值){
          金额=价值;
        }
    };
});

在第一个控制器要在serviceA量分配在projectList.total功能,而不是在它之外时。

  .controller('ProjectListCtrl',函数($范围,serviceA,项目){
    VAR projectList =这一点;
    projectList.projects =项目;
    projectList.total =功能(){
        无功总= 0;        angular.forEach(projectList.projects,功能(项目){
            总+ = project.type.cost;
            serviceA.set(总);
        });        //返回总; //想用这个总在其他控制器
    };
})

此外,serviceA是一种服务,所以你使用它像一个对象,而不是将其分配给一个变​​量

  .controller('PaymentFormCtrl',
    功能($范围,$ HTTP,serviceA){
    VAR总= serviceA.get();
    的console.log(总);});

I'm trying to share a subtotal calculated in one controller with another controller. When I use the code below, I get a total is not defined error.

I'd really appreciate suggestions on how to get this working right.

I'm using this Stackoverflow post as a model.

.factory('serviceA', function() {
   var serviceA = {
   amount: null
  };
   return serviceA;
})

.controller('ProjectListCtrl', function ($scope, serviceA, Projects) {
  var projectList = this;
  projectList.projects = Projects;
  serviceA.amount = total;

   projectList.total = function () {

   var total = 0;

   angular.forEach(projectList.projects, function (project) {
    total += project.type.cost;

    });
   return total; //want to use this total in other controller
 };
})

.controller('PaymentFormCtrl',
  function ($scope, $http, serviceA) {
  $scope.serviceA = serviceA;
  console.log(serviceA);
// . . . 
});

解决方案

You may want to use a Service in this case as services are singletons, which means that only one will be created and each controller will be using the same service, rather than them using their own factory.

.service('serviceA', function() {
    var amount = 0;
    return {
        get: function () {
            return amount;
        },
        set: function (value) {
          amount = value;
        }
    };
});

In the first controller you want to assign the amount in serviceA when in the projectList.total function rather than outside of it.

.controller('ProjectListCtrl', function ($scope, serviceA, Projects) {
    var projectList = this;
    projectList.projects = Projects;
    projectList.total = function () {
        var total = 0;

        angular.forEach(projectList.projects, function (project) {
            total += project.type.cost;
            serviceA.set(total);
        });

        //return total; //want to use this total in other controller
    };
})

Also, serviceA is a service so you use it like an object rather than assigning it to a variable

.controller('PaymentFormCtrl',
    function ($scope, $http, serviceA) {
    var total = serviceA.get();
    console.log(total);

});

这篇关于在角跨控制器共享分类汇总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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