javascript - 当按钮单击事件 ng-click 但 ng-repeat 未加载时,我正在调用 ajax

标签 javascript angularjs angularjs-scope angularjs-ng-repeat

我被称为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>

点击这里

最佳答案

$.ajaxjQuery 的一部分,但不是 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/

相关文章:

javascript - 将 Handlebars 用于 HTML 以外的模板?

javascript - 视障模式

javascript - AngularJS - 显式运行指令

javascript - 通过表格字段创建到特定页面的链接

javascript - 谷歌应用引擎中 channel api中的 channel 断开通知

javascript - 为什么Angular js不自动更新两倍的数字?

java - 为什么新建的Jhipster项目body height=0?

angularjs - 未知提供者 : $templateRequestProvider <- $templateRequest <- $route <- ngViewDirective

javascript - 从 AngularJS Controller 中,如何解析模块中定义的另一个 Controller 函数(动态 ng-controller)?

angularjs - 有没有一种好方法来管理在 $rootScope 上触发的所有事件,这样您就不会得到同名事件?