ng-repeat中当前迭代中的AngularJS Targeting元素

Sam*_*mmy 6 javascript angularjs angularjs-ng-repeat

我确定这个问题已经以一种或另一种形式得到了无数次的回答,但是我不确定要寻找什么才能找到解决方案。

假设我们有一个简单的ng-repeat:

<div ng-repeat="item in items">
   <input type="text" value="{{item.name}}">
   <a ng-click="getTxtBoxVal(whatDoIPassInHere)">Get Text Box Value</a>
</div>
Run Code Online (Sandbox Code Playgroud)

在javaScript文件中:

   function $scope.getTxtBoxVal(val) {
       alert(val)
   }
Run Code Online (Sandbox Code Playgroud)

基本上我想知道应该在whatDoIPassInHere参数中传递什么,在jquery中将是这样的:$(this).siblings(input).val()


我确实有一种解决方法,即为每个文本框赋予唯一的ID:

<input type="text" id="myTextBox_{{index}}" value="{{item.name}}>

并使用唯一的ID定位它,但是我敢肯定,有一种更优雅的方式来处理此问题

Dan*_*one 5

您没有将输入与模型相关联,因此angular不知道如何引用该值。有关如何完成您所要求的内容,请参阅此插件

控制者

$scope.things = [
    {
      'name': 'apple',
      'value': '12',
      'isTasty': true
    },
    {
      'name': 'tire',
      'value': '7',
      'isTasty': false
    },
    {
      'name': 'carrot',
      'value': '9',
      'isTasty': false
    },
    {
      'name': 'cake',
      'value': '1',
      'isTasty': true
    }
  ];

  $scope.getVal = function (value) {
    alert(value);
  };
Run Code Online (Sandbox Code Playgroud)

视图

<div ng-repeat="item in things">
      <input type="text" ng-model="item.name" value="{{item.name}}">
      <a href="#" ng-click="getVal(item.name)">Get Text Box Value</a>
    </div>
Run Code Online (Sandbox Code Playgroud)