javascript - 在 ng-repeat 中获取行的索引(或 id)以对特定行执行一些操作

标签 javascript angularjs

我是 Angular 的新手,我想获取特定行的索引以在其上执行函数 hidestuff(),我将 item.id 传递给该函数,然后我想要隐藏包含此 ID 的行女巫......但我如何通过行索引来删除整行?

html:

                  <tr ng-repeat="item in ItemsByPage[currentPage]">
                        <td>
                          <div  ng-model="tid" 
                                ng-hide="hidden" 
                                ng-class="{fade: startFade}"> 

                               {{item.id}} 
                          </div>                                
                        </td>
                        <td>
                           <div editable-text="item.name" 
                                onaftersave='inlineupdateName(item.id,item.name)' 
                                ng-model="tname" data-n='item.name'>

                                {{item.name}}
                           </div>
                        </td>
                        <td>
                           <div editable-text="item.phone" 
                                 onaftersave='inlineupdatephone(item.id,item.phone)'
                                 ng-model="tphone">
                                 {{item.phone}}
                            </div>
                        </td>
                    </tr>
                <input  type="text" ng-model="delId"   class="form-control" 
                         placeholder="Enter user id to delete th user">                        
                <button ng-click="deleteuser(delId)"  type="button" 
                        class="btn btn-primary">

                        Delete User
                </button> 

js:

$scope.hideStuff = function (delId) {
                $scope.startFade = true;
                 //here i want to use the index to delete the row
                $scope.hidden = true;
            };
            $scope.deleteuser = function (dalId) {                   
                var data = {delId : $scope.delId};
                $http.post('delete.php', data )
                  .success(function(response) {
                    $scope.hideStuff(delId);
                  });  

            };

最佳答案

您可以使用 $index 请阅读以下内容以获取更多信息: https://docs.angularjs.org/api/ng/directive/ngRepeat

我认为你应该这样做:

            <tr id="tr-{{item.id}}" ng-repeat="item in ItemsByPage[currentPage]">
                    <td>
                      <div> 
                           {{item.id}} 
                      </div>                                
                    </td>
                    <td>
                       <div editable-text="item.name" 
                            onaftersave='inlineupdateName(item.id,item.name)' 
                            ng-model="tname" data-n='item.name'>

                            {{item.name}}
                       </div>
                    </td>
                    <td>
                       <div editable-text="item.phone" 
                             onaftersave='inlineupdatephone(item.id,item.phone)'
                             ng-model="tphone">
                             {{item.phone}}
                        </div> 
                    </td>
                </tr>

             <input  type="text" ng-model="delId"   class="form-control" 
                     placeholder="Enter user id to delete th user">                        
            <button ng-click="deleteuser(delId)"  type="button" 
                    class="btn btn-primary">

                    Delete User
            </button> 

js

        $scope.hideStuff = function (delId) { 
            $("#tr-"+delId).hide();
            //the entire tr (line table) will be hidden.
            // you don't need those $scope variables to hide the elements
        };
        $scope.deleteuser = function (dalId) {                   
            var data = {delId : $scope.delId};
            $http.post('delete.php', data )
              .success(function(response) {
                $scope.hideStuff(delId);
              });  

        };

关于javascript - 在 ng-repeat 中获取行的索引(或 id)以对特定行执行一些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32912591/

相关文章:

javascript - Node.js 回调函数

javascript - 查找某个时间范围内去年的某个日期

javascript - Angular - 从 CMS 延迟加载整个页面

angularjs - orderBy 可以与 AngularUI typeahead 一起使用吗?

angularjs - 前端 API 调用和后端 API 调用任何外部后端服务器之间的区别 代码

javascript - 使用 JavaScript 来防止评估后面的 `&lt;script&gt;` 标记?

javascript - 成功将图像上传到 firebase 存储后,Firebase 获取下载 URL

javascript - 仅操作二维数组的数值

Internet Explorer 中的 Javascript 代码错误

javascript - Angular JS - 如何将 Javascript 对象导出到 XLS 文件?