2017-05-29 61 views
0

我在AngularJS开发中相当新手。我正在研究一个项目,其中有一组输入表。我已经使用ng模型来计算一个基于另外两个的输入。 例如,我有一个学生对象,其中包含正确的,不正确的和未尝试的问题。对于很多学生,我使用ng-repeat如下所示。但是,当我执行ng-model=some-expression时,它在控制台中显示错误,表示该表达式不可分配。但它的工作。我很困惑,有什么方法可以解决这个问题。 感谢AngularJS ng模型的数学表达式

HTML:

<tr ng-repeat="student in studentData"> 
    <td>{{ student.name }}</td> 
    <div ng-show="selectedSubj.indexOf('Physics') > -1"> 
     <td><input type="number" min="0" ng-model="student.correctPhysics" required> 
     <input type="number" min="0" ng-model="student.incorrectPhysics" required> 
     <input type="number" min="0" ng-model="student.unattemptedPhysics=noOfQues-student.correctPhysics-student.incorrectPhysics"></td> 
    </div> 
</tr> 

控制器代码:

$scope.studentData = ['Akhilesh']; 
for(var i = 0; i < $scope.selectedStudents.length; i++){ 
    $scope.studentData.push({name: $scope.selectedStudents[i].name, batch: $scope.selectedStudents[i].batch, 
    correctPhysics: 0, incorrectPhysics: 0, unattemptedPhysics: 0, 
    correctChemistry: 0, incorrectChemistry: 0, unattemptedChemistry: 0, 
    correctMaths: 0, incorrectMaths: 0, unattemptedMaths: 0, 
    correctBio: 0, incorrectBio: 0, unattemptedBio: 0, total: ''}); 
} 

回答

0

角度不要让你写里面ng-model表达。因此,可以在里面ng-init

<input type="number" min="0" ng-init="student.unattemptedPhysics=noOfQues-student.correctPhysics-student.incorrectPhysics" ng-model="student.unattemptedPhysics"></td> 

angular.module("app",[]) 
 
.controller("ctrl",function($scope){ 
 

 
$scope.studentData = [{"name":"Akhilesh","batch":"A1","correctPhysics":12,"incorectPhysics":3,"unatemptedPhysics":0,"crrectChemistry":0,"ncorrectChemistry":0,"unattemptedChemisry":0,"correctMaths":0,"incorrectMaths" : 0,"unattemptedMaths":0,"correctBio":0,"ncorrectBio":0,"unatemptedBio":0,"tota":45}] 
 
    
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
<table> 
 
<tr ng-repeat="student in studentData"> 
 
    <td>{{ student.name }}</td> 
 
    <div ng-show="selectedSubj.indexOf('Physics') > -1"> 
 
     <td><input type="number" min="0" ng-model="student.correctPhysics" required ng-change="student.unattemptedPhysics=noOfQues-student.correctPhysics-student.incorrectPhysics"> 
 
     <input type="number" min="0" ng-model="student.incorrectPhysics" required> 
 
     <input ng-init="student.unattemptedPhysics=noOfQues-student.correctPhysics-student.incorrectPhysics" type="number" ng-model="student.unattemptedPhysics"></td> 
 
    </div> 
 
</tr> 
 
</table> 
 
</div>

+0

感谢做到这一点。但是我试过了,它并没有在视图上实时升级它? –

+0

发布'studentData'数组 –

+0

'[{“name”:“Akhilesh”,“batch”:“A1”,“correctPhysics”:12,“incorrectPhysics”:3,“unattemptedPhysics”:0,“correctChemistry”:0 “incorrectChemistry”:0 “unattemptedChemistry”:0 “correctMaths”:0 “incorrectMaths”:0 “unattemptedMaths”:0 “correctBio”:0 “incorrectBio”:0 “unattemptedBio”:0,”总数“:45}]' –