跨服务角度共享枚举

时间:2015-09-15 09:18:45

标签: javascript angularjs enums

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

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

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

2 个答案:

答案 0 :(得分:4)

定义constant

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

并将其注入其他服务:

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

答案 1 :(得分:2)

从工厂返回枚举

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

        return {
                 DoSomething : function (param1){
                 },

                 myEnum: {
                    val1: 0,
                    val2: 1
                 }

          };
   };
}]);

您可以通过

访问
myService.myEnum;
相关问题