我有一个包含对象的现有数组,并在第一步创建了几个属性。它由以下函数创建:
$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在下一步中将另一个属性添加到现有对象中。我尝试过以下功能,但似乎没有用。
$scope.insertRecord = function (recordlist) {
var arrival = { 'arrival' : moment().format("HH.mm") }
console.log(arrival)
angular.extend($scope.recordlist, arrival)
jsonToRecordLocalStorage($scope.recordlist);
}
我正在寻找的最终结果如下:
[{
"date":"2014 10 16",
"time":"20.22",
"car":"396",
"driver":"Seb",
"from":"A",
"destination":"B",
"pax":"3",
"arrival":"23.10"
}]
另外还要考虑的另一件事是,在应用程序中,它存在列出许多不同对象的可能性,一些已经定义了arrival
属性,但有些会在以后及时生成
任何指针?
修改
我得到的2个解决方案但是没有完成我想要的工作,所以我可能不清楚我想要做什么。
我将一组对象保存到一个数组中,每个对象都有一个属性arrival
,将在第二步定义。
首先,我创建一个像这样的对象数组:
[
{
"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",
}
]
它以类似于表格的布局显示在视图上。每行包含来自对象的数据,并且最初不包含属性arrival
,因为该应用程序跟踪汽车移动并且汽车尚未到达目的地。
每一行还有一个触发insertRecord()
的按钮,用于定义到达时间,并且应该将arrival
属性独立地包含在每个对象中。
所以在这个例子中,也许来自第二个对象的汽车到了,我希望能够仅为这个特定对象添加arrival
属性,留下这样的数组:
[
{
"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个解决方案允许我既定义数组的第一个对象的到达,又为所有对象定义,但不是分别为每个对象定义。
这是调用insertData的HTML代码:
<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>
如此,有关如何到达那里的任何提示?
答案 0 :(得分:12)
根据您的代码$scope.recordlist
是一个对象数组。所以你可能想要这个:
angular.extend($scope.recordlist[0], arrival);
UPD 要更新数组中的每个对象,您可以使用map
方法:
$scope.recordlist = $scope.recordlist.map(function(obj) {
return angular.extend(obj, arrival);
});
答案 1 :(得分:8)
如果正确理解您的问题,您希望通过添加arrival
来更新单个记录。将记录传递给insertRecord
:
<button ng-click="insertRecord(record)" ng-show="!record.arrival">
您可以将该属性添加到相应的记录中:
$scope.insertRecord = function (record) {
record.arrival = moment().format("HH.mm");