【问题标题】:Using toastr in AngularJS for confirmation dialog在 AngularJS 中使用 toastr 进行确认对话框
【发布时间】:2015-12-03 18:59:57
【问题描述】:

我对 Angular 有点陌生。 在我的一个控制器中,我在 toastr 上设置了一些全局选项并调用了一个 toastr 通过工厂 (tndNotifier) 公开的方法。这似乎工作正常,但我不喜欢 事实是,当我单击 toastr 中的“继续”按钮或“x”以外的任何内容时 div,我最终调用了 onclick 事件。我试图完全忽略这一点,以便用户 不会无意中调用 onclick 事件。似乎这可能是不可能的,但我可能是错的。此外,单击按钮不会关闭 toastr 实例。

  promptForDeleteServiceLog: function(serviceLog) {
      // I am sure I can hide toastr behind some other abstraction to avoid a direct reference
      toastr.options = {
        tapToDismiss: false,
        closeButton: true,
        onclick: function(object) {
          $scope.tndServiceLogList.deleteServiceLog(serviceLog);
        }
      };
      // Embedding markup here makes me feel dirty
      tndNotifier.warn('You are about to delete ' + serviceLog.description + '. Click \'Continue\' to delete or \'x\' to cancel.' +
          ' <br><br><button class="btn btn-warning">Continue</button>');
    }

工厂是这样定义的:

angular.module('app').value('tndToastr', toastr);

angular.module('app').factory('tndNotifier', function(tndToastr) {
  return {
    notify: function(msg) {
      tndToastr.success(msg);
      console.log(msg);
    },
    warn: function(msg) {
      tndToastr.warning(msg);
      console.log(msg);
    },
    error: function(msg) {
      tndToastr.error(msg);
      console.log(msg);
    }
  }
});

我使用 toastr 的原因是它为我提供了一个通用的通知机制。我应该以这种方式使用 toastr(用于确认对话框)还是应该编写一个自定义指令来弹出一个 div 而根本不使用 toastr?我的用例的首选方法是什么?

我有一个想法,也许我应该在指令中使用 jQuery 来尝试实现与 toastr 相似的外观(和行为),但我不知道最好的方法。

This jsfiddle 说明了我上面描述的内容。

【问题讨论】:

  • toastr 不是用来输入的。它是一个通知库。您应该使用类似 angular ui bootstraps modal 指令的东西。
  • 很有意义。当我尝试通过带有嵌入式标记的 toastr 强制解决方案时,我开始以我的方式看到错误。我还没有使用 angular ui bootstrap modal,所以我会尝试一下。
  • 现在等一下,有办法解决您的问题;-)

标签: angularjs toastr


【解决方案1】:

根据你的小提琴:

您正在设置配置对象的 toastr 全局与您的 tndToastr value 提供程序不同,因此在 tndToaster 上调用事件不会尊重您传递的配置。:

// Wrong Way. 
myApp.controller('MyCtrl', function ($scope, tndNotifier) {
  $scope.tndServiceLogList = {
    promptForDeleteServiceLog: function (serviceLog) {
      toastr.options = { //This is not the same toastr variable as your injectable tndToastr that you defined, so therefore calling events on tndToaster wont resepct this config.
        tapToDismiss: false,
        closeButton: true
      };
      tndNotifier.warn('You are about to delete  FOO. Click \'Continue\' to delete or \'x\' to cancel.' +
        ' <br><br><button class="btn btn-warning">Continue</button>');
    }  };
});

注入tndToaster并配置:

如果您改为注入如下所示的值,然后在其上调用配置,您会看到您正在寻找的所需行为。

// Way that does work in updated fiddle
myApp.controller('MyCtrl', function ($scope, tndNotifier, tndToastr) {
  $scope.tndServiceLogList = {
    promptForDeleteServiceLog: function (serviceLog) {
      tndToastr.options = {
        tapToDismiss: false,
        closeButton: true
      };
      tndNotifier.warn('You are about to delete  FOO. Click \'Continue\' to delete or \'x\' to cancel.' +
        ' <br><br><button class="btn btn-warning">Continue</button>');
    }  };
});

这里是更新后的 FIDDLE 控制器。

旁注:我应该使用 toast 通知作为模式吗?

Seth Flowers 提出了一个很好的观点,即您可能不应该使用通知库来生成在 UI/UX 中看起来像模态的内容。但是,如果它像鸭子一样走路,像鸭子一样说话,那么它就是鸭子。如果它具有您需要的所有功能而不必使用它,那么我相信它可以安全使用。我建议查看现有的 ui-modal 组件或 http://ngmodules.org/ 上的其他选项。

【讨论】:

  • 感谢您的示例和解释。但是,我注意到添加和不添加对 tndToastr 提供程序的引用的相同行为(toastr 不会自动关闭)。正如我所料,这一切似乎有点笨拙。所以我很可能会转向使用 Angular UI 引导模式,因为我意识到我已经在我的应用程序中使用 Angular ui-bootstrap 作为日期选择器。
  • 如果您可以确认提供的示例不起作用,我可以继续接受您的回答。我只是不希望其他人认为 toastr 在任何方面都是模态确认对话框问题的有效解决方案。
  • 你不是说你不想让 toast 消失除非你点击了取消按钮或 X?
  • 不,我说过按钮单击不会关闭 toastr。我可以看出您可能认为我的意思是让它保持可见,但我尝试使用 'tapToDismiss: false' 是为了强制它自动关闭。
  • 我现在明白了,那么我会说你最好使用ui-bootstrap-modal。
猜你喜欢
  • 2016-10-08
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多