-1
指令通知应在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>
将notification作为notificationFactory.notifications添加到数组中。[notificationFactory.identifier] = note - working。谢了哥们。 – Rennos
虽然实际上已经意识到我有一个错字...很高兴知道它的工作,但我仍然在更新答案。 –