0

Hey guys I want to delete item by id in an AngularJS function. How can I get id from delete button?

<table data-ng-app="myApp" data-ng-controller="myController">
    <tbody>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>School</th>
            <th>Gender</th>
            <th>Email</th>
            <th>Update</th>
            <th>Delete</th>
        </tr>
        <tr data-ng-repeat="student in students">
            <td>{{student.id}}</td>
            <td>{{student.name}}</td>
            <td>{{student.school}}</td>
            <td>{{student.gender}}</td>
            <td>{{student.email}}</td>
            <td><a href="#" id="tagaUpdate" >Update</a></td>
            <td><a href="#" id="tagaDelete" data-ng-click="deleteItem({{student.id}})">Delete</a></td>
        </tr>
    </tbody>
</table> 

Controller:

var myApp = angular.module('myApp', []);
myApp.controller("myController", function ($scope) {
    $scope.deleteItem = function (id) {
        alert(id);//it doesn't show alert here
    };
});

I think the problem is here data-ng-click="deleteItem({{student.id}})". But when I check it shows me value data-ng-click="deleteItem(5).

1
  • ramamoorthy_villi's answer will solve your problem. But you are trying to to manipulate DOM from controller. But controllers are not for manipulating DOM, for this purpose you can use directive. Commented Mar 30, 2015 at 10:50

1 Answer 1

3

replace

<a href="#" id="tagaDelete" data-ng-click="deleteItem({{student.id}})">Delete</a>

by

<a href="#" id="tagaDelete" data-ng-click="deleteItem(student.id)">Delete</a>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.