如何通过AngularJS删除一个项目

How to delete one item by AngularJS?

本文关键字:一个 项目 何通过 AngularJS 删除      更新时间:2023-09-26

嘿,伙计们,我想在AngularJS函数中按id删除项目。如何从删除按钮获取id?

<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> 

控制器:

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

我认为问题出在这里data-ng-click="deleteItem({{student.id}})"。但当我检查时,它显示值data-ng-click="deleteItem(5)

替换

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

通过

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