【问题标题】:Pass value from table to input text Angularjs将值从表传递到输入文本Angularjs
【发布时间】:2015-02-27 08:56:55
【问题描述】:

我有一个带有表格的输入文本,在该表格中我从带有 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>

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

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

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

【问题讨论】:

    标签: javascript angularjs input html-table


    【解决方案1】:

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

    <input ng-model='input' type="text" placeholder="Choose"/>
    
    
    
    $scope.setSelected = function (idSelectedVote) {
            $scope.idSelectedVote = idSelectedVote;
            $scope.input=idSelectedVote;
            //alert(idSelectedVote);
        };
    


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

    你可以看到这个Fiddle,它对输入进行过滤,并在点击tr时将文本放入输入中

    【讨论】:

    • 不起作用 :( 考虑到我已经在我的输入中读取了一个 ng-model (抱歉我忘记写了)
    • @endgame 你能解决你的问题吗.. 如果使用 ng-model,我创建的那个可以正常工作.. 试试我的方法
    • 这太奇怪了...同样的情况在小提琴中尝试过,没有问题..在我的中,我可以在控制台上查看单击的项目,但我无法将其传递到输入中!!跨度>
    • 好的,现在这个jsfiddle.net/8y48q/11 与我的情况非常相似......并且不起作用
    • @End.Game.. 这个问题有不同的上下文.. 你可以发布一个不同的问题.. 如果你这样做也通知我..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    相关资源
    最近更新 更多