<!DOCTYPE html>
<
html
ng-app
=
"myApp"
>
<
head
>
<
style
>
.user-container {
border: 2px solid #333;
padding: 10px;
margin: 10px;
width: 80%;
background-color: #f0f0f0;
}
.item {
display: flex;
align-items: center;
margin: 5px 0;
}
.show-index-btn {
background-color: #007bff;
color: #fff;
border: none;
margin-left: 10px;
cursor: pointer;
}
.remove-item-btn {
background-color: #dc3545;
color: #fff;
border: none;
margin-left: 10px;
cursor: pointer;
}
.add-item-btn {
background-color: #28a745;
color: #fff;
border: none;
margin-top: 10px;
cursor: pointer;
}
.add-user-btn {
background-color: #343a40;
color: #fff;
border: none;
cursor: pointer;
margin-top: 10px;
}
.task-input {
margin: 10px 0;
}
</
style
>
<
script
src
=
</
script
>
</
head
>
<
body
ng-controller
=
"MainController"
>
<
h1
style
=
"color: green;"
>
GeeksforGeeks
</
h1
>
<
h3
>
Approach 2: Using a Function
in the Controller
</
h3
>
<
div
ng-repeat
=
"user in users"
class
=
"user-container"
>
<
h2
>User {{ $index + 1 }}</
h2
>
<
ul
>
<
li
ng-repeat
=
"task in user.tasks"
class
=
"item"
>
<
span
>{{ task.name }}</
span
>
<
button
ng-click
=
"showIndex(user, task)"
class
=
"show-index-btn"
>
Show Index
</
button
>
<
button
ng-click
=
"removeTask(user, task)"
class
=
"remove-item-btn"
>
Remove Task
</
button
>
</
li
>
</
ul
>
<
input
type
=
"text"
ng-model
=
"newTask"
placeholder
=
"Add a new task"
class
=
"task-input"
>
<
button
ng-click
=
"addTask(user, newTask)"
class
=
"add-item-btn"
>
Add Task
</
button
>
</
div
>
<
button
ng-click
=
"addUser()"
class
=
"add-user-btn"
>
Add User
</
button
>
<
script
>
var app = angular.module('myApp', []);
app.controller('MainController', function ($scope) {
$scope.users = [
{
tasks: [{ name: 'Learn AngularJS' },
{ name: 'Write GeeksforGeeks article' },
{ name: 'Practice JavaScript' }]
},
{
tasks: [{ name: 'Study Algorithms' },
{ name: 'Create Web Apps' }]
}
];
$scope.showIndex = function (user, task) {
var parentIndex =
$scope.users.indexOf(user);
var childIndex =
user.tasks.indexOf(task);
alert("User Index: " + parentIndex +
", Task Index: " + childIndex);
};
$scope.addTask = function (user, newTask) {
if (newTask) {
user.tasks.push({ name: newTask });
$scope.newTask = "";
}
};
$scope.removeTask = function (user, task) {
var index = user.tasks.indexOf(task);
if (index !== -1) {
user.tasks.splice(index, 1);
}
};
$scope.addUser = function () {
$scope.users.push({
tasks: []
});
};
});
</
script
>
</
body
>
</
html
>