【问题标题】:Unable to r-initialize AngularJs Object as per html ng-repeat objects?无法根据 html ng-repeat 对象重新初始化 AngularJs 对象?
【发布时间】:2016-09-06 18:59:17
【问题描述】:

我被卡住了,不知道如何处理这段代码。我根据要求更改了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[response.dayOfWeek] 未定义

感谢并等待回复。

【问题讨论】:

  • 重新初始化是什么意思?如果您更改变量,HTML 将跟随更改。
  • 其实我根据我的要求改了html,但是我不能改js,因为ajax调用后返回错误,我收到错误

标签: javascript angularjs object


【解决方案1】:

不知道是不是问题,但是你没有关闭函数。

$scope.saveEditSub = function(id){

【讨论】:

  • 我解释了错误,请看。 ajax 调用后我无法传递正确的对象值
【解决方案2】:

进行这样的验证:

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});

【讨论】:

  • 现在让我试试
  • 我尝试了您的代码,而不是更新 ajax 接收的值,而是创建了一个空框
  • 我们应该使用id吗?
猜你喜欢
  • 1970-01-01
  • 2017-03-17
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
  • 2019-10-14
相关资源
最近更新 更多