我正在开发一个Web应用程序,使用Laravel作为后端API,并使用AngularJS作为前端。我已经从Laravel API成功获取了数据,并通过AngularJS ng-repeat显示了它。现在我想要为表中显示的每个记录删除按钮。当用户单击该删除按钮时,它应该删除所单击的记录。我做了以下尝试,效果很好。但是当我单击删除按钮时出现了问题从数据库中删除记录,但不刷新记录列表,而是刷新表,仅显示表头标题,而不显示其他内容。当我从浏览器手动刷新它时,它会显示回记录列表。我想在删除记录后自动加载列表。
摘要:删除后,我想自动加载/刷新列表,该列表暂时没有发生。
学生负责人:
public function destroy($id)
{
$student = Student::find(Input::get('id'));
if($student){
$student->delete();
return Response::json(['destroy'=>true]);
}
}
app.js
var myApp = angular.module('myApp', ['mainCtrl', 'myAppService']);
myappservice.js
angular.module('myAppService', [])
.factory('Result', function($http) {
return {
get : function() {
return $http.get('api/result');
},
show : function(id) {
return $http.get('api/result/' + id);
},
save : function(resultData) {
return $http({
method: 'POST',
url: 'api/result',
headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
data: $.param(resultData)
});
},
destroy : function(id) {
return $http.delete('api/result/' + id,{params: {id}});
}
}
});
MainCtrl.js
angular.module('mainCtrl', [])
.controller('mainController', function($scope, $http, Result) {
// object to hold all the data for the new comment form
$scope.resultData = {};
// loading variable to show the spinning loading icon
$scope.loading = true;
// get all the comments first and bind it to the $scope.comments object
Result.get()
.success(function(data) {
$scope.students = data;
$scope.loading = false;
});
// function to handle submitting the form
$scope.submitResult = function() {
$scope.loading = true;
// save the comment. pass in comment data from the form
Result.save($scope.resultData)
.success(function(data) {
$scope.resultData = {};
// if successful, we'll need to refresh the comment list
Result.get()
.success(function(getData) {
$scope.students = getData;
$scope.loading = false;
});
})
.error(function(data) {
console.log(data);
});
};
// function to handle deleting a comment
$scope.deleteResult = function(id) {
$scope.loading = true;
Result.destroy(id)
.success(function(data) {
// if successful, we'll need to refresh the comment list
Result.get()
.success(function(getData) {
$scope.students = getData;
$scope.loading = false;
});
});
};
});
看法
<table class="table table-striped">
<thead>
<tr>
<th>Roll No</th>
<th>Student Name</th>
<th>Father Name</th>
<th>Obtained Marks</th>
<th>Total Marks</th>
<th>Percentage</th>
<th>Delete</th>
</tr>
</thead>
<tbody ng-hide="loading" ng-repeat="student in students | filter:searchText">
<tr>
<td>@{{ student.rollno }}</td>
<td>@{{ student.name }}</td>
<td>@{{ student.fname }}</td>
<td>@{{ student.obtainedmarks }}</td>
<td>@{{ student.totalmarks }}</td>
<td>@{{ student.percentage }}</td>
<td>
<a href="#" ng-click="deleteResult(student.id)" class="text-muted btn-danger btn-lg">Delete</a></p>
</td>
</tr>
</tbody>
</table>
控制台错误:DELETE http:// localhost / ngresulty / public / api / result / 30?id = 30 500(内部服务器错误)
我的猜测是该Result.get()
呼叫未返回正确的数据。您可能要检查该端点。
但是,由于您知道在删除时会发生成功事件,因此不必再进行另一个调用,您可以先更改范围,然后再不调用后端,即:
// function to handle deleting a comment
$scope.deleteResult = function(id) {
$scope.loading = true;
Result.destroy(id)
.success(function(data) {
// do something with data if you want to
$scope.students.splice(id, 1);
});
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句