javascript – Angularjs:使用ngChange时如何获取列表中的项?

我试图在这个小例子中得到刚改变的项目,是否有某种上下文我应该使用?我希望它只是简单地引用$this,但这似乎不起作用.

<html ng-app>
<head>
<script src="https://code.angularjs.org/1.2.9/angular.min.js"></script>
<script>
  function myController($scope) {
    $scope.items = [
      { id: 1, name: "First" },
      { id: 2, name: "Second" },
      { id: 3, name: "Third" }
    ];

    $scope.test = function() {
        // How do I get the object in the list related to the change i did?
        // I.e. "{ id: 2, name: "Second" }" for the second item.
    };
  }
</script>
</head>
<body>
<div ng-controller="myController">
<table>
  <tbody ng-repeat="item in items">
    <tr>
      <td>{{ item.id }}</td>
      <td><input type="text" ng-model="item.name" ng-change="test()"/></td>
    </tr>
  </tbody>
</table>
</div>
</body>

最佳答案 看看这个

Working Demo

HTML

<div ng-app='myApp' ng-controller="myController">
<table>
  <tbody ng-repeat="item in items">
    <tr>
      <td>{{ item.id }}</td>
      <td><input type="text" ng-model="item.name" ng-change="test(item.name)"/></td>
    </tr>
  </tbody>
</table>
</div>

脚本

var app = angular.module('myApp', []);
app.controller('myController', function ($scope) {
    $scope.items = [
      { id: 1, name: "First" },
      { id: 2, name: "Second" },
      { id: 3, name: "Third" }
    ];

    $scope.test = function(item) {
        alert(item);
    };
});
点赞