2017-05-29 12 views
0

私はAngularJS開発のかなりの初心者です。私は入力のセットを持つテーブルを持っているプロジェクトに取り組んでいます。私は他の2つの入力に基づいて1つの入力を計算するためにng-modelを使用しました。 たとえば、正しく、間違っていて、未解決の質問をキーとして学生オブジェクトがあります。多くの学生にとって、私はng-repeatを次のように使用しています。しかし、ng-model=some-expressionを実行すると、コンソールに式が割り当てられないというエラーが表示されます。しかし、それは動作します。私はこれを解決する方法があるのか​​混乱しています。 おかげでAngularJS ng-modelへの数式

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 0、 "correctMaths":0、 "incorrectMaths":0、 "unattemptedMaths":0、 "correctBio":0、 "incorrectBio":0、 "unattemptedBio":0、 "wrongChemistry":0、 "unattemptedChemistry" total ":45}]' –

関連する問題