Angularjs:面临ng-change和ng-iterate的问题

时间:2017-09-12 04:06:17

标签: javascript html angularjs angularjs-ng-change

问题描述:

  1. 每当我尝试从dropdown1中选择选修时,相同的主题代码和&主题学分也将填入第二选修课。
  2. 每当我尝试选择下拉列表的最后一个值时,主题代码&主题信用保持不变(即显示先前的值)。
  3. 我浏览了与此方案相关的帖子上的堆栈溢出。但是,我无法找到解决方案,因为这种情况非常具体。这就是为什么我发布这个问题的原因。

    我是angularjs的完全新手。任何帮助将不胜感激。提前谢谢!

    CalculateResult.html:

    <tr ng-repeat="x in subjects">
    <td ng-if="x.subjectCode.indexOf('ELECTIVE') == -1">{{ x.subjectCode }}</td>
    <td ng-if="x.subjectName.indexOf('ELECTIVE') == -1">{{ x.subjectName }}</td>            
    <td ng-if="x.subjectCode.indexOf('ELECTIVE') != -1">{{ elective.electiveSubjectCode }}</td>
    <td ng-if="x.subjectName.indexOf('ELECTIVE') != -1" ng-model="elective" ng-init="increment()">
        <select class="form-control" ng-init="elective = electives[index][0]" ng-model="elective" ng-options="elective.electiveSubjectName for elective in electives[{{index}}]" ng-change="changeSelectedItem(elective.electiveSubjectCode,elective.electiveCredit)" required>
        </select>
    </td>
    </tr>
    

    CalculateResult.js:

    var app=angular.module('myApp',[]);
    app.controller('myCtrl',function($scope,$http,$window){
      $scope.title = sessionStorage.getItem('title');
      var length = sessionStorage.getItem('length');
      $scope.subjects = [];
      for(var i=0;i<length;i++)
      {
    $scope.subjects.push({subjectCode:sessionStorage.getItem('subjectCode'+i),subjectName:sessionStorage.getItem('subjectName'+i),credit:sessionStorage.getItem ('credit'+i)});
      }     
    
      $scope.noOfElectives = sessionStorage.getItem('noOfElectives');
      $scope.electives = [];
      $scope.electiveArray = [];
      $scope.elective = {};
      for(var i=1;i<=$scope.noOfElectives;i++)
      {
          $scope.electiveArray = [];  
          for(var j=1;j<=sessionStorage.getItem('electiveLength'+i);j++)
          {
            $scope.elective = {electiveSubjectCode:sessionStorage.getItem('electiveSubjectCode'+i+j),electiveSubjectName:sessionStorage.getItem('electiveSubjectName'+i+j),electiveCredit:sessionStorage.getItem('electiveCredit'+i+j)};
            $scope.electiveArray.push($scope.elective);
          }
          $scope.electives.push($scope.electiveArray);
      }   
    $scope.elective.electiveSubjectCode = '--';
    $scope.elective.electiveCredit = '--';
    $scope.index = -1; 
    $scope.increment=function(){
      $scope.index = $scope.index + 1;
    }    
    $scope.changeSelectedItem=function(electiveSubjectCode,electiveCredit){ 
      $scope.elective.electiveSubjectCode = electiveSubjectCode;
      $scope.elective.electiveCredit = electiveCredit; 
    }});
    

    Problem - 1: Whenever I try to select elective from 1st dropdown, the subject code & subject credit gets populated for 2nd elective as well.

    Problem - 2: Whenever I try to select the last value of dropdown, the subject code and subject credit remains unchanged (i.e., previous value is displayed)

0 个答案:

没有答案