简体   繁体   English

跨服务共享枚举

[英]Sharing enums across services angular

I have an enum in one of my services. 我在一项服务中有一个枚举。

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

I need to share this enum when I call a method in some other service. 在其他服务中调用方法时,我需要共享此枚举。 Basically need to send the enum as a parameter to some other method in another service. 基本上需要将枚举作为参数发送给另一个服务中的某些其他方法。 What is the best approach to do so ? 这样做的最佳方法是什么?

Define a constant : 定义一个constant

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

And inject it to other services: 并将其注入其他服务:

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

return enum from your factory 从工厂返回枚举

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

        return {
                 DoSomething : function (param1){
                 },

                 myEnum: {
                    val1: 0,
                    val2: 1
                 }

          };
   };
}]);

you can access by 您可以通过访问

myService.myEnum;

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

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