将值从 table 传递到输入文本 Angularjs

Pass value from table to input text Angularjs

我有一个带有 table 的输入文本,其中我在输入的同时使用 ng-repeat 从 JSON 中过滤了一些值。

<input type="text" placeholder="Choose" ng-model="item.sTxt"/>
   <table>
      <thead>
         <tr>
            <th>First Value</th>
            <th>Second Value</th>
         </tr>
      </thead>
      <tbody>
         <tr
             data-ng-repeat="item in numberList.getList | filter:searchText" 
             ng-click="setSelected(item.last)">
             <td>{{item.first}}</td>
             <td>{{item.last}}</td>
         </tr>
      </tbody>
  </table>

我可以通过这种方式在 table 中单击一行时显示警报:

$scope.idSelectedVote = null;
    $scope.setSelected = function (idSelectedVote) {
        $scope.idSelectedVote = idSelectedVote;
        alert(idSelectedVote);
    }; 

但我会采用该值并将其传递到我的输入文本中。我怎么能在 angularjs 中做到这一点?

您可以使用 ng-model 在文本输入上创建模型,然后只需将单击的 table 行的值传递给该模型,就像这样

<input ng-model='input' type="text" placeholder="Choose"/>



$scope.setSelected = function (idSelectedVote) {
        $scope.idSelectedVote = idSelectedVote;
        $scope.input=idSelectedVote;
        //alert(idSelectedVote);
    };


另外如果你想过滤掉重复的列表,你可以使用相同的模型,在过滤器中。

你可以看到这个 Fiddle,它既过滤输入又在点击 tr 时在输入中放置文本