29

Currently, I just call toastr.success('my message') within a controller where required. This work fine, but it feels a bit dirty to me.

Is there a 'best practice' or recommended 'angularjs' way of using the toastr.js library?

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
link64
  • 1,944
  • 1
  • 26
  • 41

1 Answers1

61

Yes. Pretty simply:

app.factory('notificationFactory', function () {
    return {
        success: function (text) {
            toastr.success(text,"Success");
        },
        error: function (text) {
            toastr.error(text, "Error");
        }
    };
});

Resolve factory in controller. Customize messages, notifications/etc in factory.

Despite the idea that code adds another abstraction, it's really effective.

Drakes
  • 23,254
  • 3
  • 51
  • 94
Eugene P.
  • 2,645
  • 19
  • 23