使用AngularJS angular.extend独立地向数组的每个对象添加属性 [英] Adding property to each object of an array independently with AngularJS angular.extend

查看:44
本文介绍了使用AngularJS angular.extend独立地向数组的每个对象添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含对象的现有数组,并在第一步创建了几个属性。它由以下函数创建:

I have an existing array with an object and several properties created on a first step. It is created by the following function:

$scope.recordlist = extractRecordJSONFromLocalStorage();
$scope.addRecord = function () {
    $scope.recordlist.push(
            {
                date: $scope.dateJson, 
                time: $scope.time, 
                car: $scope.carList.code, 
                driver: $scope.driverList.name,
                from: $scope.locationList.place,
                destination: $scope.locationList2.place, 
                pax: $scope.paxList
            }
        );

    $scope.custom = $scope.custom === false ? true: false;
    $scope.carList = 0;
    $scope.driverList = 0;
    $scope.locationList = 0;
    $scope.locationList2 = 0;
    jsonToRecordLocalStorage($scope.recordlist);
};

它产生以下数组:

[{
  "date":"2014 10 16",
  "time":"20.22",
  "car":"396",
  "driver":"Seb",
  "from":"A",
  "destination":"B",
  "pax":"3"
}]

我要做的是将另一个属性添加到现有对象上下一步用ng-click。我尝试过以下功能,但它似乎不起作用。

What I'm trying to do is to add another property into the existing object on the next step with ng-click. I've tried with the following function but it doesn't seem to work.

$scope.insertRecord = function (recordlist) {

    var arrival = { 'arrival' : moment().format("HH.mm") }
    console.log(arrival)
    angular.extend($scope.recordlist, arrival)


    jsonToRecordLocalStorage($scope.recordlist);
}

我正在寻找的最终结果如下:

The end result I'm looking for is the following:

[{
  "date":"2014 10 16",
  "time":"20.22",
  "car":"396",
  "driver":"Seb",
  "from":"A",
  "destination":"B",
  "pax":"3",
  "arrival":"23.10"
}]

也许另外需要注意的是,在应用程序中,它存在列出许多不同对象的可能性,其中一些已经定义了到达属性,但有些会及时发布。

Maybe another thing to take also into consideration is that in the app it exists the possibility of having many different objects being listed, some that have the arrival property already defined, but some that will have it later in time.

任何指针?

EDIT

我得到的2个解决方案但没有完成我想要的工作,所以可能我不清楚我想要做什么。

The 2 solutions I got worked but didn't accomplish what I am looking for, so probably I was unclear on what I am trying to do.

我将一组对象保存到一个数组中,每个对象都有一个属性到达,将在第二步定义。

I am saving a collection of objects into an array, each object has a property arrival that will be defined on a second step.

首先我创建了一个这样的对象数组:

So first I create an array of objects like this:

[
 {
  "date":"2014 10 16",
  "time":"20.42",
  "car":"396",
  "driver":"Seb",
  "from":"A",
  "destination":"B",
  "pax":"3",
 },
 {
  "date":"2014 10 16",
  "time":"20.12",
  "car":"319",
  "driver":"Seb",
  "from":"C",
  "destination":"D",
  "pax":"4",
 },
 {
  "date":"2014 10 16",
  "time":"20.22",
  "car":"396",
  "driver":"Seb",
  "from":"G",
  "destination":"A",
  "pax":"1",
 }
]

这在视图中以类似于表格的布局显示。每行包含来自对象的数据,并且最初不包括属性到达,因为该应用程序跟踪汽车移动并且汽车尚未到达目的地。

This is displayed on the view in a table-like layout. Each row contains the data from an object, and initially doesn't include the property arrival because the app tracks car movements and the car has not arrived to destination.

每行还有一个触发 insertRecord()的按钮,用于定义到达时间及其应包含的内容每个对象的到达属性。独立。

Each row has also a button triggering insertRecord(), that defines the arrival time and its supposed to include the arrival property into each object, independently.

所以在这个例子中,也许第二个对象的汽车到了,我希望能够仅为此特定对象添加到达属性,将数组保留为:

So in this example, maybe the car from the second object arrived, and I want to be able to add the arrival property only for this particular object, leaving the array like this:

[
 {
  "date":"2014 10 16",
  "time":"20.42",
  "car":"396",
  "driver":"Seb",
  "from":"A",
  "destination":"B",
  "pax":"3"
 },
 {
  "date":"2014 10 16",
  "time":"20.12",
  "car":"319",
  "driver":"Seb",
  "from":"C",
  "destination":"D",
  "pax":"4",
  "arrival":"20.35"
 },
 {
  "date":"2014 10 16",
  "time":"20.22",
  "car":"396",
  "driver":"Seb",
  "from":"G",
  "destination":"A",
  "pax":"1"
 }
]

dfsq提出的2个解决方案允许我同时使用定义数组的第一个对象的到达,或者同时为所有对象定义,但不是分别为每个对象定义。

The 2 proposed solutions from dfsq allowed me to both define arrival for the first object of the array, or for all the objects at once, but not for each object separately.

这是调用insertData的HTML代码:

This is the HTML code where insertData is called:

<div class="row msf-row" ng-repeat="record in recordlist | filter: search">
      <div class="col-md-1">{{record.time}}</div>
      <div class="col-md-1"><strong>{{record.car}}</strong></div>
      <div class="col-md-1">{{record.driver}}</div>
      <div class="col-md-3">{{record.from}}</div>
      <div class="col-md-3">{{record.destination}}</div>
      <div class="col-md-1">{{record.pax}}</div>
      <div class="col-md-1">
           <button ng-click="insertRecord()" ng-show="!record.arrival"><i class="fa fa-cog"></i></button>{{record.arrival}}
      </div>
</div>   

这样,有关如何到达那里的任何提示吗?

Being so, any hints on how to get there?

推荐答案

如果正确理解您的问题,您想通过添加到达来更新单个记录。将记录传递给 insertRecord

If understand your question correctly you want to update a single record by adding arrival. Pass the record to insertRecord:

<button ng-click="insertRecord(record)" ng-show="!record.arrival">

在那里你可以将房产添加到相应的记录中:

There you can add the property to the respective record:

$scope.insertRecord = function (record) {
  record.arrival =  moment().format("HH.mm");

这篇关于使用AngularJS angular.extend独立地向数组的每个对象添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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