gpt4 book ai didi

javascript - 如何从子数组中删除数据?

转载 作者:行者123 更新时间:2023-11-28 00:33:02 25 4
gpt4 key购买 nike

在下表中我有两个小问题!

第一:

添加和删除行程效果很好。也许有人可以给我一个提示,如何从子数组中删除我的条目 - 一次旅行中的天数?

第二:

当我创建新行程时,我无法在其中创建新的天数。我认为这是因为缺少数组 DAYS,该数组在添加新行程时未创建。我已经搜索了几个小时,但找不到有效的解决方案!

抱歉,我对这些东西很陌生......

这是我的代码和 plunker url

http://plnkr.co/edit/MRAhLk7NxZHx4mTLaYxt?p=preview

index.html

<body ng-controller="TripController">
<div class="row" style="margin-top: 50px;">
<div class="col-md-10 col-md-offset-1">
<form name="addTrip" ng-submit="addTrip()">
<input ng-model="newtrip" type="text" name="newtrip" placeholder="YYYY-MM-DD" required />
<button ng-disabled="addTrip.$invalid">Add Trip</button>
</form>

<div class="alert alert-info" role="alert" ng-repeat="trip in trips">
<h5>Startdatum: {{trip.Startdate | date:'dd.MM.yyyy'}} <a class="pull-right" ng-click="deleteTrip($index)">x</a></h5>
<table class="table">
<tr>
<th><a href="" ng-click="predicate = 'DATE'; reverse=reverse">DATE</a>
</th>
<th></th>
<th></th>
</tr>
<tr ng-repeat="day in trip.DAYS | orderBy:predicate:!reverse" style="background-color: #CCC;">
<td width="25%;">{{day.DATE | date:'dd.MM.yyyy'}}</td>
<td width="25%;">
<input ng-model="day.IATA" />
</td>
<td width="25%;">
<input ng-model="day.DUTY" />
</td>
<td width="25%;">
<a ng-click="deleteDay()"></a>
</td>
</tr>
</table>
<form name="dayForm" ng-controller="DayController as dayCtrl" ng-submit="dayCtrl.addDay(trip)">
<div class="row">
<div class="col-xs-3 col-md-3">{{dayCtrl.day.DATE | date:'dd.MM.yyyy'}}</div>
<div class="col-xs-3 col-md-3">{{dayCtrl.day.IATA}}</div>
<div class="col-xs-3 col-md-3">{{dayCtrl.day.DUTY}}</div>
</div>
<div class="row">
<div class="col-xs-3 col-md-3">
<input ng-model="dayCtrl.day.DATE" type="text" class="form-control" placeholder="DATE (YYYY-MM-DD)" title="DATE" required />
</div>
<div class="col-xs-3 col-md-3">
<input ng-model="dayCtrl.day.IATA" type="text" class="form-control" placeholder="IATA" title="IATA" />
</div>
<div class="col-xs-3 col-md-3">
<input ng-model="dayCtrl.day.DUTY" type="text" class="form-control" placeholder="DUTY" title="DUTY" />
</div>
<div class="col-xs-3 col-md-3">
<button type="submit" ng-disabled="addDay.$invalid" class="btn btn-primary">Add</button>
</div>
</div>
</form>
</div>
</div>
</div>
{{trips}}
</body>

脚本.js

(function() {
var app = angular.module('showTrips', []);


app.controller('TripController', ['$scope', '$http', function ($scope, $http){
$http.get('trips.json').success(function(data) {

$scope.trips = data;

$scope.addTrip = function(){
$scope.trips.push({'Startdate':$scope.newtrip})
$scope.newtrip = ''
}

$scope.deleteTrip = function(index){
$scope.trips.splice(index, 1);
}


});


}]);


app.controller("DayController", function(){

this.day = {};
this.addDay = function(trip)
{
trip.DAYS.push(this.day);
this.day = {};
}

});

})();

最佳答案

怎么样:

1)

$scope.delTripDay = function(trip, index) {
trip.DAYS.splice(index, 1);
}

<a ng-click="delTripDay(trip, $index)">DEL</a>

2)

$scope.trips.push({'Startdate':$scope.newtrip, DAYS: []})

Working Plunkr

关于javascript - 如何从子数组中删除数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28785033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com