2016-07-07 36 views
1

我想從SQL中獲取數據並使用ng-repeat選項在HTML表格中顯示,然後我需要編輯表格單元格中的一些值。我的問題是,我只在控制器中獲得初始值,並且這些更改未反映在控制器中。這裏是我的代碼:Angular JS,多個插入錯誤

app.controller('CRUD_EntryController', function ($scope, CRUD_InternalEntryService) { 

GetStudentMarkDetails(); 

function GetStudentMarkDetails() { 
    var PromiseGetMarks = CRUD_InternalEntryService.GetMarkDetails(); 
    PromiseGetMarks.then(function (res) { 
     $scope.MarkList = res.data; 
    }) 

} 
$scope.mark = {}; 
$scope.save = function (MarkList) { 

    var index = 0; 
    $scope.MarkList.forEach(function (mark) { 
     console.log('rows #' + (index++) + ': ' + JSON.stringify(mark)); 
     alert(mark.M1); 
    } 

} 

查看:

<table class=" table table-condensed" id="myresul"> 
     <tr> 
      <th>Slno</th> 
      <th>Name</th> 
      <th>RegNo</th> 
      <th>ClassNo</th> 
      <th>M1</th> 
      <th>M2</th> 
      <th>M3</th> 
     </tr> 

     <tbody data-ng-repeat="mark in MarkList" > 
      <tr> 
      <td class="col-md-1" >#{{$index+1}}</td> 
      <td class="col-md-2" ng-model="mark.Fname">{{mark.Fname}}</td> 
      <td class="col-md-2">{{mark.RegNo}}</td> 
      <td class="col-md-1">{{mark.ClassNo}}</td> 
      <td class="col-md-1"><input type="number" value="{{mark.M1}}" ng-model="M1" class="form-control" /></td> 
      <td class="col-md-1"><input type="number" value="{{mark.M2}}" ng-model="M2" class="form-control" /></td> 
      <td class="col-md-1"><input type="number" value="{{mark.M3}}" ng-model="M3" class="form-control" /></td> 

      </tr> 
      <button data-ng-click="save(MarkList)" class="btn btn-success">Save</button> 
     </tbody> 

    </table> 

回答

0

不要以爲你需要定義這樣的:$scope.mark = {};,因爲標記在你的NG-重複的範圍設定。將其刪除是因爲這有點令人困惑,並且可能在將來導致錯誤。

刪除value="{{mark.M1}}"並將您的模型綁定到ng-model="{{mark.M1}}"。假設你魔杖在輸入中綁定到M1,M2和M3。

有關更多詳細信息,請參閱angular docs for ngModel並相應地更新您的代碼。

通過你不必通過MarkList作爲論據Save(..)的方式,你可以這樣做: <button data-ng-click="save()" class="btn btn-success">Save</button>,該Save方法簽名更改爲Save()和使用$scope.MarkList而不是爭論MarkList的。

或者改變你的方法只保存特定的mark而不是每次都保存整個列表。

+0

謝謝,它適用於我 – jomon

+0

沒問題!如果問題解決了,請將其標記爲答案。 –