繁体   English   中英

跨服务共享枚举

[英]Sharing enums across services angular

我在一项服务中有一个枚举。

home.factory('myService', ['$dialogs', '$resource', 
function ($dialogs, $resource) {
    var myEnum= {
        val1: 0,
        val2: 1
    };
    return {
        DoSomething : function (param1) {
            ...
        }
    };
}]);

在其他服务中调用方法时,我需要共享此枚举。 基本上需要将枚举作为参数发送给另一个服务中的某些其他方法。 这样做的最佳方法是什么?

定义一个constant

app.constant('myEnum', {
    val1: 0,
    val2: 1
});

并将其注入其他服务:

app.service('myService', ['myEnum', function (myEnum) {
    console.log(myEnum);
}]);

从工厂返回枚举

home.factory('myService', ['$dialogs', '$resource', ,
function ($dialogs, $resource) {

        return {
                 DoSomething : function (param1){
                 },

                 myEnum: {
                    val1: 0,
                    val2: 1
                 }

          };
   };
}]);

您可以通过访问

myService.myEnum;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM