无法按照html的ng-repeat对象来初始化AngularJs对象

Unable to r-initialize AngularJs Object as per html ng-repeat objects?

本文关键字:对象 初始化 AngularJs ng-repeat html      更新时间:2023-09-26

我被卡住了,不知道如何处理这段代码。我根据要求更改了html,但ajax响应后,我无法在Angula Js中重新初始化对象值,如果有人可以帮助,我将感激他。

<div ng-repeat="(key,sch) in classSchedule">
        <h2>{{sch.sectionName}}</h2>
        <table class="table table-bordered table-hover" >
          <tbody>
            <tr>
                <th style="width: 10px">{{phrase.Day}}</th>
                <th>{{phrase.ClassSchedule}}</th>
                <th ng-if="userRole == 'admin'" style="width: 10px">Add</th>
            </tr>
            <tr ng-repeat="subSch in sch.schedule">
                <td>{{subSch.dayName}}</td>
                <td>
                  <div class="btn-group" ng-repeat="subSubSch in subSch.sub">
                      <button type="button" class="btn btn-default">{{subSubSch.subjectId}} - {{subSubSch.start}} -> {{subSubSch.end}}</button>
                      <button ng-if="userRole == 'admin'" type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                          <span class="caret"></span>
                          <span class="sr-only">{{phrase.toggleDropdown}}</span>
                      </button>
                      <ul ng-if="userRole == 'admin'" class="dropdown-menu" role="menu">
                        <li><a ng-click="editSubOne(subSubSch.id,key)">{{phrase.Edit}}</a></li>
                        <li><a ng-click="removeSub(subSubSch.id,key)">{{phrase.Remove}}</a></li>
                      </ul>
                  </div>
                </td>
                <td ng-if="userRole == 'admin'">
                  <a ng-click="addSubOne(key)" type="button" class="btn btn-info btn-flat"><i class="fa fa-fw fa-plus"></i></a>
                </td>
            </tr>
          </tbody>
        </table>
    </div>
    <script>
    $scope.saveEditSub = function(id){
    showHideLoad();
    dataFactory.httpRequest('classschedule/sub/'+id,'POST',{},$scope.form).then(function(data) {
  response = apiResponse(data,'edit');    
  if(data.status == "success"){
      for (x in $scope.classSchedule[$scope.oldDay].schedule.sub) {
          if($scope.classSchedule[$scope.oldDay].schedule.sub[x].id == id){
            $scope.classSchedule[$scope.oldDay].schedule.sub.splice(x,1);
          }
      }
      if(! $scope.classSchedule[response.dayOfWeek].schedule.sub){
          $scope.classSchedule[response.dayOfWeek].schedule.sub = [];
      }
      $scope.classSchedule[response.dayOfWeek].schedule.sub.push({"id":response.id,"classId":response.classId,"subjectId":response.subjectId,"start":response.startTime,"end":response.endTime});
  }
  $scope.scheduleModalEdit = !$scope.scheduleModalEdit;
  showHideLoad(true);
});
</script>

Ajax调用后错误:-> $ scope.classSchedule[响应。dayOfWeek]未定义

谢谢,等待响应

我不知道这是否导致了问题,但是您没有关闭该函数。

$scope.saveEditSub = function(id){

创建如下验证:

if(!$scope.classSchedule[response.dayOfWeek]){
   $scope.classSchedule[response.dayOfWeek] = {
      schedule: {sub : [] }
   }
}

然后继续代码:

if(! $scope.classSchedule[response.dayOfWeek].schedule.sub){
          $scope.classSchedule[response.dayOfWeek].schedule.sub = [];
      }
      $scope.classSchedule[response.dayOfWeek].schedule.sub.push({"id":response.id,"classId":response.classId,"subjectId":response.subjectId,"start":response.startTime,"end":response.endTime});