我有一个现有的数组,其中包含一个对象,并在第一步创建了几个属性.它由以下函数创建:
$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","pax":"3","arrival":"23.10" }]
也许还需要考虑的另一件事是,在应用程序中,它存在列出许多不同对象的可能性,一些已经定义了到达属性,但有些会在以后及时使用它.
有什么指针吗?
编辑
我得到的2个解决方案但是没有完成我想要的工作,所以我可能不清楚我想要做什么.
我将一组对象保存到一个数组中,每个对象都有一个属性到达,将在第二步定义.
所以首先我创建一个这样的对象数组:
[ { "date":"2014 10 16","time":"20.42",},{ "date":"2014 10 16","time":"20.12","car":"319","from":"C","destination":"D","pax":"4","from":"G","destination":"A","pax":"1",} ]
它以类似于表格的布局显示在视图上.每行包含来自对象的数据,并且最初不包括属性到达,因为应用程序跟踪汽车移动并且汽车尚未到达目的地.
每一行还有一个触发insertRecord()的按钮,它定义了到达时间,并且它应该独立地将到达属性包含在每个对象中.
所以在这个例子中,也许来自第二个对象的汽车到了,我希望能够只为这个特定的对象添加到达属性,留下这样的数组:
[ { "date":"2014 10 16","pax":"3" },"arrival":"20.35" },"pax":"1" } ]
dfsq提出的两个解决方案允许我既定义数组的第一个对象的到达,又一次定义所有对象的到达,但不是分别为每个对象定义.
<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>
如此,任何有关如何到达那里的提示?