map绑定ng-model不更新数据

时间:2014-10-09 09:07:11

标签: angularjs map

我调用了REST服务,它给了我一个包含地图的对象。 java中的地图看起来像地图

以下是我的js

$scope.marks = {};
//get data from rest

StudentService.query().$promise.then(function(data) 
{
  $scope.students = data;
  for(var i=0;i<$scope.students.length;i++){
  var obj = $scope.students[i];
  //marks (key=studentName, value=mark in decimal)
  $scope.marks[obj["studentName"]]=0.0;
}

以下是我的HTML

<div class="row" ng-repeat="(key,value) in marks">

    <input type="text" class="form-control" ng-model="key" disabled>

{{marks[key]}} <!-- Here it is not updating value from above model-->

    <input type="number" class="form-control" ng-model="value">


</div>

当我在textfield中更新值时,它不是在textfiled下面显示的更新值,即{{marks[key]}}未显示更新的值。如果错误请纠正我。谢谢:))

1 个答案:

答案 0 :(得分:2)

您传递给ng-model的只是一个字符串,它是不可变的。您需要像这样定义ng-model

<input type="number" class="form-control" ng-model="marks[key]">

angular.module('app', [])
  .controller('Ctrl', function($scope) {
    $scope.marks = {
      mark1: 1,
      mark2: 2,
      mark3: 3
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Ctrl">
  <div class="row" ng-repeat="(key,value) in marks">

    <input type="text" class="form-control" ng-model="key" disabled>{{marks[key]}}
    <!-- Here it is not updating value from above model-->

    <input type="number" class="form-control" ng-model="marks[key]">

  </div>
</div>