Using toastr in the AngularJS way

倾然丶 夕夏残阳落幕 提交于 2019-12-21 03:11:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/21565738/using-toastr-in-the-angularjs-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!