我有一个“通知”应用程序,其中显示可能有与之关联的通知的不同应用程序。如果应用程序没有通知,则只会显示应用程序本身。加载应用程序后,我希望检查是否有与应用程序关联的通知。如果不存在通知,那么我希望不显示或删除应用程序。我不确定我的代码有什么问题。
我相信我需要在 listenForNotifications
或 getApps
中添加一些代码来检查是否有,如果没有,则“删除”应用程序。 getApps
首先被点击并拉取用户可能拥有的应用程序。那么也许可以检查一下是否有任何通知与该应用程序关联?我以为我走在正确的轨道上,但它仍然不起作用。
Controller
function NotificationsCtrl($scope, $state, Service, SecService, RecService) {
var vm = this;
vm.ctrlName = 'NotificationsCtrl';
$scope.showMenu = false;
$scope.appList = {};
$scope.dismissNotification = function(notification) {
EcosystemService.dismissNotification(notification.id).then(
function (response) {
delete $scope.appList[notification.appId].notifications[notification.id];
});
};
$scope.dismissNotifications = function(app) {
var notifications = app.notifications;
for (var id in notifications) {
EcosystemService.dismissNotification(notifications[id].id).then(
function(notificationId) {
return function (response) {
delete app.notifications[notificationId];
}
}(id));
}
};
$scope.init = function () {
Service.getUserAppsByEmail(SecService.secState.username).then(function (response) {
var apps = response.applications;
$scope.appList['notes'] = {
name: 'Notes',
notifications: {}
} ;
for(var i = 0; i < apps.length; i++) {
$scope.appList[apps[i].appId] = {
name: apps[i].name,
icon: apps[i].icon,
notifications: {}
}
}
});
};
function listenForNotifications() {
RealtimeService.on("notifications", function (data) {
var app = $scope.appList[data.body.appId];
if (app != null) {
if(!data.body.dismissed) {
data.body.unixEpoch = Date.parse(data.body.whenCreated);
app.notifications[data.body.id] = data.body;
}
else {
delete app.notifications[data.body.id];
delete app.appList[data.body.appId];
}
}
else {
delete app.appList[data.body.appId];
}
});
}
最佳答案
您可以执行以下操作:
<div ng-repeat="app in appList" ng-if="app.notifications.length > 0">
.......
</div>
<div ng-if="noNotifications()">No notifications!</div>
$scope.noNotifications = function() {
var foundNotifications = true;
angular.forEach($scope.appList, function(app, index) {
foundNotifications = foundNotifications && app.notifications && app.notifications.length > 0;
});
return !foundNotifications;
};
编辑:我通过检查 $scope.noNotifications()
函数添加了一个更简单的解决方案来显示“无通知”。
关于javascript - 如果没有使用 JavaScript 连接到应用程序的通知,则删除应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34513912/