如果没有使用JavaScript连接到应用程序的通知,请删除应用程序
我有一个“通知”应用程序,其中显示可能有与之关联的通知的不同应用程序。如果应用程序没有通知,则只显示应用程序本身。加载应用程序后,我希望检查是否有与应用程序关联的通知。如果不存在通知,则我不希望显示或删除这些应用。我不确定我的代码出了什么问题 我认为我需要在如果没有使用JavaScript连接到应用程序的通知,请删除应用程序,javascript,html,angularjs,Javascript,Html,Angularjs,我有一个“通知”应用程序,其中显示可能有与之关联的通知的不同应用程序。如果应用程序没有通知,则只显示应用程序本身。加载应用程序后,我希望检查是否有与应用程序关联的通知。如果不存在通知,则我不希望显示或删除这些应用。我不确定我的代码出了什么问题 我认为我需要在listenForNotifications或getApps中添加一些代码,以检查是否有,如果没有,然后“删除”这些应用getApps首先被点击,并拉入用户可能拥有的应用程序。那么,也许可以检查一下,看看是否有任何通知与该应用程序相关?我以为
listenForNotifications
或getApps
中添加一些代码,以检查是否有,如果没有,然后“删除”这些应用getApps
首先被点击,并拉入用户可能拥有的应用程序。那么,也许可以检查一下,看看是否有任何通知与该应用程序相关?我以为我走对了路,但它仍然不起作用
控制器
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];
}
});
}
函数通知SCTRL($scope、$state、Service、SecService、RecService){
var vm=这个;
vm.ctrlName='NotificationsCtrl';
$scope.showMenu=false;
$scope.appList={};
$scope.dismissNotification=函数(通知){
EcosystemService.dismissNotification(notification.id)。然后(
功能(响应){
删除$scope.appList[notification.appId].notification[notification.id];
});
};
$scope.dismissNotifications=函数(应用程序){
var通知=app.notifications;
for(通知中的变量id){
EcosystemService.dismissNotification(通知[id].id)。然后(
函数(notificationId){
返回函数(响应){
删除应用程序通知[notificationId];
}
}(id));
}
};
$scope.init=函数(){
Service.getUserAppsByMail(SecService.secState.username)。然后(函数(响应){
var apps=response.applications;
$scope.appList['notes']={
名称:'注释',
通知:{}
} ;
对于(变量i=0;i
您可以执行以下操作:
<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=函数(){
var=true;
angular.forEach($scope.appList,函数(应用程序,索引){
foundNotifications=foundNotifications&&app.notifications&&app.notifications.length>0;
});
返回!创建通知;
};
编辑:我通过检查
$scope.noNotifications()
函数添加了一个更简单的显示“无通知”的解决方案。这就是最终解决问题的方法。。。
将此添加到HTML中
<div ng-repeat="app in appList" ng-show="(app.notifications | AssociativeArrayFilter) > 0">
这是一个简单的解决方案。谢谢!如果没有可用的应用程序,我也想显示一条消息。我可以将其也包括在HTML中吗?@jednerd-我添加了一些应该可以工作的东西,因为我现在无法在本地测试它。这会删除应用程序,但当“警报”有通知时,它仍然不会显示。在
listenfornotificate中ions()
,如果(!data.body.discomered)
等于false。因此,即使有通知,也会阻止应用程序显示。因此,我认为还需要做更多的工作。尽管如此,我觉得答案指向了正确的方向。仍然无法正常运行。没有显示任何应用程序。我将通知插入警报应用程序中,因此应该显示它,但仍然没有应用程序正在放映。
angular.module('notifications').filter('AssociativeArrayFilter', function () {
return function (collection) {
if (!collection) {
return 0;
}
var reCount = 0;
angular.forEach(collection, function (val, index) {
reCount++;
});
return reCount;
}
})