在AngularJS方式使用toastr [英] Using toastr in the AngularJS way

查看:275
本文介绍了在AngularJS方式使用toastr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我只是叫 toastr.success如有需要一个控制器内('我的信息')。这项工作很好,但感觉有点脏了我。

有一个最佳实践或者推荐angularjs使用 toastr.js库 <? / p>

解决方案

是的。 pretty简单:

  app.factory('notificationFactory',函数(){
    返回{
        成功:函数(文本){
            toastr.success(文字,成功);
        },
        错误:功能(文字){
            toastr.error(文字,错误);
        }
    };
});

解决工厂控制器。自定义消息,通知/等工厂。

尽管code中的想法增加了一个抽象,它是真正有效的。

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?

解决方案

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.

这篇关于在AngularJS方式使用toastr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆