已成功从数据库中删除数据,但未刷新AngularJS中的当前页面

Data deleted from DB successfully but not refreshed the current page in AngularJS

本文关键字:AngularJS 刷新 当前页 成功 数据库 删除 数据      更新时间:2023-09-26

我尝试了几次删除数据,它成功地删除了数据,但没有显示更新的学生完整列表。。。。。

手动刷新网页后,它会显示更新的学生名单,请帮助我。

  
X.controller('StudentController', ['$scope', 'Student', 'Department', '$routeParams','$location', function ($scope, Student, Department, $routeParams,$location) {
    $scope.v = 'test value';
    
    $scope.profileid = $routeParams.Id;
    console.log($scope.profileid);
        ////////////////RETRIVE ALL DATA FROM DB///////////////
    function init() {
        /////RETRIVE DATA FOR STUDENT/////////
   
        $scope.student = { Name: '', Phone: '', Class_id: '', Department_id: '' };
        $scope.students = [];
        Student.get(function (response) {
            console.log("List of Student");
            $scope.students = response.Data;
            console.log($scope.students);
        });
        ///////RETRIVE DATA FOR DEPARTMENT////////////////////
        $scope.depts = [];
        Department.get(function (response) {
            console.log("List of Dept");
            $scope.depts = response.DeptData;
            console.log($scope.depts);
        });
    };
////////////RETRIVE SPECIFIC DATA INTO DB/////////////
Student.get({ Id: $scope.profileid }, function (response) {
        console.log("SPECIFIC DATA");
        $scope.student = response.Data;
        console.log($scope.students);
    });
/////////////////////////////DELETE//////////////////
    $scope.Stddelete = function (profileId) {
        Student.delete({ Id: profileId });
        console.log("DELETE");
        init();
    };
    init();
    }
]);

要更新DOM,需要从数组中splice对象。

$scope.students.splice($index,1);

示例:

$scope.Stddelete = function (profileId,index) {
        Student.delete({ Id: profileId });
        $scope.students.splice(index,1);
        console.log("DELETE");
        init();
    };