我被称为ajax按钮点击,ng-repeat不会一次点击加载数据,但是当我点击两次其加载时,我不知道为什么请检查代码
$scope.myData.doClick = function (item,event) {
var startdate = document.getElementById('txtstart').value;
var enddate = document.getElementById('txtend').value;
$.ajax({
type: "POST",
url: "studentattendance.aspx/GetEmployees",
data: JSON.stringify({ title: startdate, songname: enddate}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$scope.studentattendance = {};
$scope.studentattendance = JSON.parse(msg.d);
console.log($scope.studentattendance);
},
error: function (msg) {
alert(msg.d);
}
});
}
//HTML
<tr ng-repeat="info in studentattendance">
<td>{{info.student_name}}</td>
<td>{{info.father_name}}</td>
<td>{{info.class_name}}</td>
<td>{{info.attendancedate | date:'dd-MM-yyyy'}}</td>
<td ng-if="info.attendanceType == 'Present'"> <button type="button" class="btn btn-success btn-circle"><i class="fa fa-check"></i></td>
<td ng-if="info.attendanceType == 'Absent'"> <button type="button" class="btn btn-danger btn-circle"><i class="fa fa-times"></i></td>
<td ng-if="info.attendanceType == 'Leave'"> <button type="button" class="btn btn-warning btn-circle"><i class="fa fa-list"></i></td>
</tr>
点击这里
最佳答案
$.ajax
是 jQuery
的一部分,但不是 Angular
的一部分。因此,当它从您的 ajax 请求返回时, Angular 上下文不知道您的数据发生变化。当您再次单击时,将强制运行摘要循环,并在此时在数组中检测到新数据 - 这就是您看到它再次呈现的原因。
您有两个选择。
1- 在成功回调中使用 $scope.$apply()
。
喜欢
success: function (msg) {
$scope.studentattendance = {};
$scope.studentattendance = JSON.parse(msg.d);
console.log($scope.studentattendance);
$scope.$apply();
}
2- 使用 Angular 的 $http
服务 - 它是一个内置服务,会在内部为您调用 $scope.$apply()
喜欢
$http({
method: "POST",
url: "studentattendance.aspx/GetEmployees",
dataType: 'json'
data: JSON.stringify({ title: startdate, songname: enddate}),
headers: { "Content-Type": "application/json; charset=utf-8" },
}).then(function (msg) {
$scope.studentattendance = {};
$scope.studentattendance = JSON.parse(msg.d);
console.log($scope.studentattendance);
}, function (msg) {
alert(msg.d);
});
我会选择第二种
关于javascript - 当按钮单击事件 ng-click 但 ng-repeat 未加载时,我正在调用 ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42796521/