【问题标题】:how to detect which table row is clicked when ng-repeat is on the <tr> tag | Angularjs当 ng-repeat 在 <tr> 标签上时如何检测点击了哪个表行Angularjs
【发布时间】:2017-12-22 07:48:44
【问题描述】:

我有一个带有输入列的表格行,我想获取一个 td 的值(id),由于某种原因,我必须通过在单击 td 时调用一个函数来完成

HTML CODE:

<tr ng-repeat="fr in nonConfirmedFactureRetour">
    <td> 
//storing id in the data-item Attr.

        <input id="pending_item_name" data-item="{{fr.id}}" value="{{fr.facture_item_name}}" type="text"/>
    </td>
    <td>
        <input id="pending_cls_crt" value="{{fr.fcls_crt}}"  type="text"/>
    </td>
    <td>
        <input id="pending_piece" value="{{fr.fpiece}}"  type="text"/>
    </td>
    <td>
        <input id="pending_dateCom" value="{{fr.dateCom}}"  type="text"/>
    </td>
    <td>
        <input id="pending_dateRec" value="{{fr.dateRec}}"  type="text"/>
    </td>
    <td>
// calling function to hget the id 
        <input ng-click="confirmEntity_retour();" type="button" value="Confirm"/>
    </td>
</tr>

JS代码:

$scope.confirmEntity_retour=function(){
            var item_nameId=$("#rfi").attr("data-itid");
            alert(item_nameId); //alert the id when calling this function
}

我希望得到输入 attr 的 id。点击提交时 但问题是,无论点击什么 td,我总是得到第一个 id

【问题讨论】:

    标签: javascript jquery html angularjs-ng-repeat angularjs-ng-click


    【解决方案1】:

    使用 JQuery 从 DOM 获取数据是违反 AngularJS 的范式的。您应该改用数据绑定。您可以将项目的 ID 作为参数传递给 confirmEntity_retour 函数。

    HTML 模板:

    <input ng-click="confirmEntity_retour(fr.id);" type="button" value="Confirm"/>
    

    控制器:

    $scope.confirmEntity_retour = function(id) {
      alert(id);
    }
    

    【讨论】:

      【解决方案2】:

      尝试将整个项目作为参数传递给函数。

      $scope.confirmEntity_retour=function(item){
              alert(item.id)
      }
      
      
      
      <input ng-click="confirmEntity_retour(fr);" type="button" value="Confirm"/>
      

      【讨论】:

        猜你喜欢
        • 2013-01-10
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 2013-11-23
        • 1970-01-01
        • 2016-03-28
        • 2022-01-26
        • 1970-01-01
        相关资源
        最近更新 更多