4

我正在尝试获取在这个小示例中刚刚更改的项目,我应该使用某种上下文吗?我希望它就像引用 $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>
4

1 回答 1

2

看看这个

工作演示

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);
    };
});
于 2014-05-12T14:25:10.510 回答