简体   繁体   中英

How to remove each element from ng-repeat with a specific timeout

How to remove the values from the ng-repeat array with a timeout from when they are added.

$timeout(function() {
     $scope.datas.splice($scope.datas[$scope.datas.length - 1]) // just something
}, 4000);

What I needed is.
If I add an element to that array only that element has to be removed after the timeout specified. not the last added element. So each element will have their own timeout

Here is a plunker for some help. https://plnkr.co/edit/5TtcwRqiXGeAOddn5wPF?p=preview .

I have no clue how to achieve that. A Little help needed.

Thanks.

You can use a timeout in your addValue function. Inside of this timeout you get the index of the added element and you remove it like this :

$scope.addValue = function() {
    var element = $scope.datas[$scope.datas.length - 1] + 1;
    $scope.datas.push(element);
    $timeout(function(){
        var index = $scope.datas.indexOf(element);
        $scope.datas.splice(index, 1)
    }, 2000);
};

Here is a working plunker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM