删除元素后的Angular JS-Update ng-repeat列表

时间:2014-07-28 11:13:17

标签: javascript angularjs

元素删除工作正常。问题是ng-repeat不会自动更新。 (我必须刷新页面以反映更改。)

 <tr ng-repeat="error in filterErrors=(latestErrors
 | filter:{ Source:SearchSource} ) 
 | orderBy:predicate:reverse
 | filter:{ ErrorId:SearchErrorId,Application:SearchApplication} 
 | startFrom:currentPage*pageSize| limitTo: pageSize">

                <td ><input type="checkbox" ng-model="error.checked" ng-click="deletingIsReady()"/></td>
                <td >{{error.Host}}</td>
                <th >{{error.Type}}</th>
                <th >{{error.Source}}</th>

            </tr>

服务:

 errorReporter.factory('dataService', function ($http,$q) {


    var _deleteErrors = function (arrayOfErrors) {
        var deferred3 = $q.defer();
        var dataString = {
            arrayOfErrors: arrayOfErrors
        }

        $http.post(ROOT + 'Home/DeleteErrors', dataString, { cache: false }).
           then(function (data, status, headers, config) {
               deferred3.resolve();
           }, function () {
               deferred3.reject();
           });
        return deferred3.promise;

    }

return {
    getLatestErrors: _getLatestErrors,
    }

});

控制器功能:

dataService.deleteErrors(arrayOfErrors).then(function () {
            var elementId=0;
            angular.forEach($scope.filterErrors, function (object) {
                elementId++;
                angular.forEach(arrayOfErrors, function (obj,index) {

                    if (object.ErrorId == obj)
                    {
                         console.log($scope.filterErrors.length);

                            $scope.filterErrors.splice(elementId, 1);

                        console.log($scope.filterErrors.length);

                    }
                })
            });
        })

切片工作正常,我进入console.log,例如100,99 .Notice控制台记录上面几行,但无论如何我的ng-repeat没有更新。

1 个答案:

答案 0 :(得分:0)

您可以将JavaScript length属性重置为比以前少1,并且应该更新。例如,如果要重复使用5个键(无论是字符串还是对象)的数组,在删除其中一个键之后,将数组长度设置为4。

var array = [
    {value: 'value1', label: 'Value 1'},
    {value: 'value2', label: 'Value 2'},
    {value: 'value3', label: 'Value 3'},
    {value: 'value4', label: 'Value 4'},
    {value: 'value5', label: 'Value 5'}
];   
var originalLength = array.length; // will equal 5

// Loop through and delete value3:
for (var i = 0; i < array.length; i++) {
    if (array[i].value === 'value3') {
        delete array[i];
    }
}

// Reset the array length manually:
array.length = originalLength - 1;

带有ng-repeat的DOM应该更新。