Javascript ng repeat中的Angularjs orderBy正在影响索引。如何从数组中删除正确的元素?

Javascript ng repeat中的Angularjs orderBy正在影响索引。如何从数组中删除正确的元素?,javascript,angularjs,Javascript,Angularjs,我有一个在表中显示的值数组。当我没有订购它们时,我的删除代码完全按照预期工作。我想对ng repeat中的值进行排序,以便按角色名称对数据进行分组,但这会使我的splice函数删除错误的值。删除所选值的正确方法是什么 工作正常的Html: <tbody data-ng-repeat="oneUserRole in ui.userRoleResultsList track by $index"> <tr> <td>{{oneUserRole.

我有一个在表中显示的值数组。当我没有订购它们时,我的删除代码完全按照预期工作。我想对ng repeat中的值进行排序,以便按角色名称对数据进行分组,但这会使我的splice函数删除错误的值。删除所选值的正确方法是什么

工作正常的Html:

<tbody data-ng-repeat="oneUserRole in ui.userRoleResultsList track by $index">
    <tr>
      <td>{{oneUserRole.UserId}}</td>
      <td>{{oneUserRole.RoleName}}</td>
      <td>{{oneUserRole.Details}}</td>
      <td class="text-center">
         <button type="button" class="btn btn-sm btn-danger" title="Delete" data-ng-click="ui.removeUserRole($index)"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
      <td>
    </tr>
<tbody>
如果你愿意改变

}


这应该行得通,我想我最后尝试了Jelle和Frank Modica的建议。感谢你们每个人的快速反应。最有效的方法是预过滤来自控制器的列表,而不是更改html

下面是我的控制器函数,它将数据获取到$scope.ui.userRoleResultsList中:

    // ******************************************************************
    // Get the existing active User Roles 
    // ******************************************************************
    function LoadUserRoleResultsList() {
        var errorSummary = 'Error retrieving UserRoles list';
        $scope.ui.userRoleResultsList = [];
        $scope.promise =
            $http.get(appConfig.GetRootUrl() + '/AdminUserRoles/UserRoleList')
                .then(function onSuccess(response) {

                    var result = apiResponseParser.Parse(response);
                    if (result.isSuccessful && result.data !== null) {
                        // presort by rolename.
                        $scope.ui.ddlUserRoleTypes = result.data;
                        $scope.ui.userRoleResultsList = $filter('orderBy')($scope.ui.ddlUserRoleTypes, 'RoleName');
                    }
                    else {
                        ReportErrorMessage(errorSummary, result.message); 
                    }
                })
            .catch(function onError(response) {
                console.log(response, " = caught response Error");
                    ReportHttpError(errorSummary, response);
                });
    }

神奇之处在于//presort by rolename注释后面的两行。

1-你把用户传递进来,而不是索引,怎么样?然后在列表中找到用户的索引。2-你可以在你的控制器中对列表进行排序,然后在排序的列表上重复
ng
(这可能也会更有效,因为排序不会发生在每个摘要上)。@FrankModica,我理解这个理论。我不知道该怎么做。请查看以下答案,以了解在控制器中使用一次
orderBy
过滤器的示例:
$scope.ui.removeUserRole = function (index) {
   // remove row from array.
   $scope.ui.userRoleResultsList.splice(index, 1);
  // other code to remove selected item from db omitted here.
}
<button type="button" class="btn btn-sm btn-danger" title="Delete" data-ng-click="ui.removeUserRole({oneUserRole.UserId)"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
$scope.ui.removeUserRole = function (userId) {    
for(let i = $scope.ui.userRoleResultsList.length; i > 0; i--) {
    if(userId == $scope.ui.userRoleResultsList[i]) {
        $scope.ui.userRoleResultsList.splice(i, 1);  
        break;   
    }
}  
    // ******************************************************************
    // Get the existing active User Roles 
    // ******************************************************************
    function LoadUserRoleResultsList() {
        var errorSummary = 'Error retrieving UserRoles list';
        $scope.ui.userRoleResultsList = [];
        $scope.promise =
            $http.get(appConfig.GetRootUrl() + '/AdminUserRoles/UserRoleList')
                .then(function onSuccess(response) {

                    var result = apiResponseParser.Parse(response);
                    if (result.isSuccessful && result.data !== null) {
                        // presort by rolename.
                        $scope.ui.ddlUserRoleTypes = result.data;
                        $scope.ui.userRoleResultsList = $filter('orderBy')($scope.ui.ddlUserRoleTypes, 'RoleName');
                    }
                    else {
                        ReportErrorMessage(errorSummary, result.message); 
                    }
                })
            .catch(function onError(response) {
                console.log(response, " = caught response Error");
                    ReportHttpError(errorSummary, response);
                });
    }