【问题标题】:Remove item based on time counter(expired date) Angularjs根据时间计数器删除项目(过期日期)Angularjs
【发布时间】:2023-03-17 15:15:01
【问题描述】:

我有一个项目列表,其中每个项目的日期以 (YYYYMMDDhhmmss) 格式关联,并希望在每张卡上附加一些事件,以查明项目日期是否已过期,如果已过期,则需要删除该项目。更像是为每个项目计算时间。

最好的方法是什么?

jsfiddle.net/tomalex0/jc6nE/2/

【问题讨论】:

标签: angularjs


【解决方案1】:

我能够实现该功能。

工作演示可以在这里找到

http://jsfiddle.net/tomalex0/jc6nE/8/

请更改日期时间以查看实时体验。

一个问题是,是否最好分拆不同的超时时间? 如果项目很多,会不会造成一些内存问题?

var arrayval= [{
    "title" : "Title 1",
    "expire_date" :  "04/30/2014 10:055 PM"
},{
    "title" : "Title 2",
    "expire_date" : "04/30/2014 09:56 PM"
},{
    "title" : "Title 3",
    "expire_date" : "04/30/2014 09:57 PM"
},{
    "title" : "Title 4",
   "expire_date" : "04/30/2014 09:58 PM"
}];

控制器

function TodoCtrl($scope, $filter) {

 $scope.sorted = arrayval;
}

指令

angular.module('counterApp', []).directive('relativeTime', function($timeout) {

function update(scope, element) {
    var  timeDiff =  moment(scope.actualTime).diff(moment())
    element.find(".item-ticker").html(timeDiff);


   console.log("inside update",scope)
    var timeoutVar = $timeout(function() {
        update(scope, element);
    }, 1000);

    if(timeDiff < 0 ){
        element.remove();
        $timeout.cancel(timeoutVar);
        console.log("ithappened",scope);
    }
}

return {
    scope: {
        actualTime: '=relativeTime'
    },
    link: function(scope, element) {
        update(scope, element);
    }
};
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-30
    • 2021-12-15
    • 2014-09-22
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    相关资源
    最近更新 更多