【发布时间】: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,所以我会尝试一下。
-
现在等一下,有办法解决您的问题;-)