2017-05-29 62 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}]' –