从范围更改获取数组

时间:2014-12-02 17:45:41

标签: javascript angularjs

这是我的代码,如何从输入框中的数组更改中获取名称??? 如何在所有数组中获取所有更改的名称???先谢谢了。马可。

app.js

var g[];
var names = ['John', 'Steve', 'Mark', 'George'];

for (var i = 1; i <= 4; i++){
  g.push({myCount: i, myName: names[i]});
};

$scope.allnames = g;

$scope.Calculate = function (??????) { 
   console.log(' INDEX = ' + myCount ???????);   
   console.log(' CHANGE NAME = ' + myName ???????);

};

的index.html

  <div class="row" ng-repeat="testx in allnames">
    <input type="text" class="form-control" ng-model="testx.myCount"/>
    <input type="text" class="form-control" ng-model="testx.myName" ng-blur="Calculate(?????)" />
  </div>                        

2 个答案:

答案 0 :(得分:0)

使用$ index获取ng-repeat中的索引

<div class="row" ng-repeat="testx in allnames">
   <input type="text" class="form-control" value="{{testx.myCount}}"/>
   <input type="text" class="form-control" ng-model="testx.myName" />
</div>

它有效.. !!

答案 1 :(得分:0)

模型的所有更改都存储在$scope.allnames中,因此,为了Caculate()已编辑对象的值,您需要对其进行引用。在此示例中,由于对象存储在数组中,因此$index提供的ng-repeat变量为我们提供了我们所需的内容。

<强> HTML

<div class="row" ng-repeat="testx in allnames">
  <input type="text" class="form-control" ng-model="testx.myCount"/>
  <input type="text" class="form-control" ng-model="testx.myName" ng-blur="Calculate($index)" />
</div>

<强>的JavaScript

$scope.Calculate = function (index) { 
  // get the associated object from $scope.allnames
  var person = $scope.allnames[index];

  console.log(' INDEX = ' + person.myCount);   
  console.log(' CHANGE NAME = ' + person.myName);
};
相关问题