简体   繁体   English

Angular JS:将对象从一个数组存储到不同的数组/服务

[英]Angular JS: Store objects from one array to different arrays/services

I just started to develop a pretty simple app in Angular JS with a service/factory that holds data that I'm able to push objects to. 我刚刚开始在Angular JS中开发一个带有服务/工厂的非常简单的应用程序,其中包含可以将对象推入的数据。

I have an array of different people (see html) that you can add as a candidates using the addCandidate() function located in the factory. 我有一系列不同的人(请参见html),您可以使用工厂中的addCandidate()函数添加为候选人。

My problem is that I want to be able to store people/objects from that array into more than one array, eg candidates2 and candidates3 depending on which route/controller is active. 我的问题是,我希望能够将人员/对象从该阵列存储到多个阵列中,例如候选人2和候选人3,具体取决于激活的路由/控制器。

Am I on the right track or should I think completely different? 我是在正确的道路上还是应该完全不同?

Factory and controllers: 工厂和控制器:

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

app.factory('Candidates', function (){

  var candidates = [];

  /* I want to fill this array with objects from myCtrl2 */
  var candidates2 = [];

  return{

    addCandidate: function(candidate){
      candidates.push(candidate);
    }

    /* More functions related to Candidates */

  }
});

app.controller('myCtrl1', function($scope, Candidates){

  $scope.addCandidate = function(candidate){
   Candidates.addCandidate(candidate);
  }

});

/* I want to push candidates to candidates2 here */

app.controller('myCtrl2', function($scope, Candidates){

  $scope.addCandidate = function(candidate2){
   Candidates.addCandidate(candidate2);
  }

});

/* More controllers of same kind */

HTML: HTML:

<ul>
    <li ng-repeat="person in people">
      <h2>{{ person.name }}</h2>
      <button ng-click="addCandidate(person)">Add candidate</button>
    </li>
</ul>

It looks like you want candidates to be different for each controller. 看来您希望每个控制器的候选人都不同。 In that case, make the factory build an object constructor (similar to a class, but not really) rather than giving the same object every time. 在这种情况下,请让工厂构建一个对象构造函数(类似于类,但实际上不是),而不是每次都提供相同的对象。

See this plunker for an example. 有关示例,请参见此插件 Notice that each controller requests and news it's own Candidate, rather than using a shared Candidate definition. 请注意,每个控制器都请求和新闻它自己的候选者,而不是使用共享的候选者定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据条件将一个JS数组存储到不同的数组中 - How to store one JS array into different arrays based on condition 在一个对象数组中结合两个不同对象数组的不同属性 - Combining different properties of two different arrays of objects in one array of objects 从角度js中的不同模块注入服务 - Injecting Services from different modules in angular js 从两个不同的数组创建对象数组 - Create an array of objects from two different arrays Angular - 比较两个 arrays 具有相同数量的对象但具有不同的值,并返回与第二个数组的差异 - Angular - Compare two arrays with same number of objects but with different values and return the difference from second array TS(JS 问题)- 获取 2 个对象数组返回 4 arrays - 每个对象都匹配和不匹配 - TS (JS question) - Get 2 array of objects return 4 arrays - matches and unmatches from each one 如果一个属性也是 Angular 7 中的一个数组,则比较两个对象的 arrays? - Comparing two arrays of objects if one property is also an array in Angular 7? Arrays 数组到对象数组 JS - Array of Arrays to Array of Objects JS 使用Node JS将多个数组转换为一个数组的对象 - Multiple arrays transformed to objects to one array with Node JS JS:如何将来自两个不同数组的对象合并为一个数组/对象 - JS: How to combine Object from two different arrays into one Array/Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM