简体   繁体   中英

Get array from scope changed

this is my code, how to get name from array change in my inputbox??? How to get all my changed name in an all array??? Thank's in advanced. Marco.

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>                        

use $index to get index in 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>

It works..!!

All changes to your model are stored in $scope.allnames , so, in order to Caculate() the values of the edited object, you need a reference to it. In this example, since the objects are stored in an array, the $index variable provided with ng-repeat gives us exactly what we need.

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);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM