【问题标题】:How to show dialog box if the value is only equal to 1?如果值仅等于 1,如何显示对话框?
【发布时间】:2021-02-05 00:09:01
【问题描述】:

现在,当我打开网站时,我的对话框就会打开,但我只是想知道如何让对话框只有在 notificationType 等于 1 时才打开。

有多种类型的通知,我只希望在 notificationType 等于 1 时打开对话框,那么如何检查我的 getNotification 调用?任何建议或帮助将不胜感激。

我的 get 调用中可以有多种类型的通知,我想查找所有索引并检查是否有任何 notificationType 1,如果有则显示对话框。如果所有索引中都没有notificationType 1,则不显示对话框。

  ngOnInit(): void {
    
    this.notificationService.getNotification().subscribe((response: any) => {
      this.broadcastText.next(response);
      if(response && response.length){
        this.dialog.open(ReleaseDialogComponent, {
          data : {notifications: response}
        });
      }

    })

【问题讨论】:

  • ...检查响应,看看是否notificationType === 1,如果是,请致电this.dialog.open
  • 嗨..我已经更新了我的问题,希望现在更清楚了。感谢您的反馈。

标签: html angular typescript filter angular-material


【解决方案1】:

if(response && response.length && response[0].notificationType === 1) 应该可以解决问题。

编辑: 根据响应,如果您想避免执行响应 [0],并假设您必须打开对话框,如果 notificacionType === 1 存在响应的任何项目,您可以执行以下操作:

if(response && response.length) {
  // This will return true if notificationType === 1 is any response item
  const shouldShowNotification = response.some(el => el.notificationType === 1);

  if (shouldShowNotification) {
    this.dialog.open(ReleaseDialogComponent, {
          data : {notifications: response}
    });
  }
}

如果尽管有通知类型,对话框仍然打开,恐怕你还有另一个问题。

【讨论】:

  • 遗憾的是这不起作用。由于某种原因,它仍然显示对话框。 ;(。还有如何不做响应[0],因为有时我在一次通话中收到多种通知类型,0索引可以是notificationType 2,索引3可以是notifactionType 1。
  • @AaseZi 这就是问题中应该包含的重要细节......
  • @AaseZi 我更新了响应以处理多个通知作为响应。不幸的是,如果对话框仍然显示,可能是你调用它的方式有问题
  • @RamiroP 它工作......非常感谢。非常感谢您的帮助。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多