سؤال

I'm trying to get the item that just changed in this small example, is there some kind of context I should use? I hoped it would be as simple as just referring to $this, but that doesn't seem to work.

<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>
هل كانت مفيدة؟

المحلول

Take a look at this

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>

script

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);
    };
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top