我想創建代表程序的多層對象,你可以看到我在做什麼這裏http://codepen.io/Irish1/pen/lbjdw以前刪除具有較小索引的對象時,刪除對象失敗。
說我加3周,每3天每3屆。我嘗試之前,我可以,只要我不刪除這些對象移除說,第二天和第三天
去除這裏的會話是代碼
HTML
<div ng-app="trainercompare">
<div ng-controller="programsController">
<form ng-submit="addProgram()" name='form' novalidate>
<p>
a program
</p>
<button type ="button" ng-click="addWeek()"> add week</button>
<!-- create weeks -->
<div ng-repeat="week in program.weeks" ng-init="weekindex = $index">
<p>
a week
</p>
<!-- create days -->
<div ng-repeat="day in week.days" ng-init="dayindex = $index">
<p>
a day
</p>
<!-- create sessions -->
<div ng-repeat="session in day.sessions" ng-init="sessionindex = $index">
<h2>a Session</h2>
<button type ="button" ng-click="removeSession(weekindex, dayindex, sessionindex)"> Remove Session</button>
</div>
<button type ="button" ng-click="addSession(weekindex, dayindex)"> Add Session</button>
<button type ="button" ng-click="removeDay(weekindex, dayindex)"> Remove day</button>
</div>
<button type ="button" ng-click="addDay($index)"> Add Day</button>
<button type ="button" ng-click="remove($index)"> Remove week</button>
</div>
<button type="submit"> add program</button>
</form>
</div>
</div>
app.js
var myModule = angular.module("trainercompare", ['ui.bootstrap']);
function programsController($scope, $http) {
var numweeks = 1;
$scope.program = {
};
$scope.addWeek = function() {
if (isDefined($scope.program.weeks)) {
$scope.program.weeks.push(
{
}
);
} else {
$scope.program = {
weeks: [
{
}
]
};
}
};
$scope.addDay = function(index) {
if (isDefined($scope.program.weeks[index].days)) {
$scope.program.weeks[index].days.push(
{
}
);
} else {
$scope.program.weeks[index] = {
days: [
{
}
]
};
}
};
$scope.addSession = function(weekindex, dayindex) {
if (isDefined($scope.program.weeks[weekindex].days[dayindex].sessions)) {
$scope.program.weeks[weekindex].days[dayindex].sessions.push(
{
}
);
} else {
$scope.program.weeks[weekindex].days[dayindex] = {
sessions: [
{
}
]
};
}
};
$scope.remove = function(index) {
$scope.program.weeks.splice(index, 1);
console.log(index);
};
$scope.removeDay = function(weekindex, dayindex) {
$scope.program.weeks[weekindex].days.splice(dayindex, 1);
console.log(dayindex);
};
$scope.removeSession = function(weekindex, dayindex, sessionindex) {
$scope.program.weeks[weekindex].days[dayindex].sessions.splice(dayindex, 1);
console.log(sessionindex);
};
function isDefined(x) {
return x !== undefined;
}
$scope.addProgram = function() {
console.log($scope.program);
$http.post('/programs', $scope.program).success(function(data, status) {
if(isDefined(data.errors)) {
console.log(data.errors);
$scope.errors = data.errors;
}
if(isDefined(data.success)) {
console.log(data.success);
$scope.errors = [];
}
});
};
}
你說,你是不是能夠只要它的前一個兄弟犯規存在刪除某些對象之前? – ProllyGeek
如果我刪除一個對象,我不能刪除後續兄弟姐妹的子女。我相信可以刪除兄弟姐妹。 – Ir1sh