【问题标题】:Deletion of elements in array with $timeout does not work properly使用 $timeout 删除数组中的元素无法正常工作
【发布时间】:2015-10-15 02:21:48
【问题描述】:

指令通知应在 5 秒后删除“自身”。但是,有些元素会丢失,有些元素会被多次删除。每个通知的标识符属性都是唯一的。感谢您的帮助。

工厂

angular.module('AdS').factory('notificationFactory', function () {
var notificationFactory = {};
notificationFactory.notifications = [];
notificationFactory.identifier =0;
notificationFactory.add = function(note){
    if(typeof note!=='undefined'){
        notificationFactory.identifier++;
        note.identifier = notificationFactory.identifier;
        notificationFactory.notifications.push(note);
    }

}
notificationFactory.delete = function (note) {
    if(typeof note!=='undefined'){     
        for(var i =0;i<notificationFactory.notifications.length;i++){
            if(notificationFactory.notifications[i].identifier==note.identifier){
                notificationFactory.notifications.splice(i,1);

            }
        }
    }

     return "";
}

notificationFactory.getNotifications = function () {
    return notificationFactory.notifications;
}

return notificationFactory;
});

指令

angular.module('AdS').directive('siteNotification', [
'$timeout',
function ($timeout) {
    return {
        restric: "E",
        templateUrl: "/Templates/htmlBits/notification.html",
        scope: {

            note:"=",
            center:"="
        },
        link: function (scope, element, attrs) {  
        $timeout(function () {           
                scope.center.delete(scope.note);

            }, 5000);



            scope.delete=function(note){

                scope.center.delete(note);
            }



        }
    };
}

]);

html

 <site-notification ng-repeat="not in notificationCenter.notifications track by $index" center=notificationCenter note=not ></site-notification>

【问题讨论】:

    标签: javascript arrays angularjs angularjs-directive


    【解决方案1】:

    您可以使用一个对象,而不是为notificationFactory.notifications 使用数组,它的唯一标识符指向您的笔记,如下所示:

    notificationFactory.notifications = {};
    notificationFactory.add = function(note) {
        if (typeof note!=='undefined') {
            notificationFactory.identifier++;
            notificationFactory.notifications[notificationFactory.identifier] = note;
        }
    }
    
    notificationFactory.delete = function (note) {
        if(typeof note!=='undefined'){     
            notificationFactory.notifications[notificationFactory.identifier] = null;
        }
        return "";
    }
    

    此外,在您的指令中,您似乎正在通过 html 注入 notificationFactory。这是很不寻常的,典型的注入你的工厂的方式如下:

    angular.module('AdS').directive('siteNotification', [
        '$timeout',
        'notificationFactory',
        function ($timeout, notificationFactory) {
            ...
        }
    

    我可以看到以不同方式执行此操作的唯一原因是您是否希望能够将不同类型的工厂注入到您的指令中。

    【讨论】:

    • 向数组添加通知作为 notificationFactory.notifications.[notificationFactory.identifier] = note - 工作。谢谢老兄。
    • 虽然实际上意识到我有一个错字......很高兴知道它有效,但我仍在更新答案。
    猜你喜欢
    • 2019-06-27
    • 2015-11-07
    • 2012-01-17
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多