Is there a way in AngularJS to define constants with other constants?

前端 未结 6 697
刺人心
刺人心 2020-12-12 10:56

I\'m trying to define constants with other constants, but it seems that it can\'t be done, because the initial constant isn\'t ready when the required constant depending req

6条回答
  •  醉话见心
    2020-12-12 11:30

    As long as you don't need access to your constant in providers, this should work fine:

    .constant('HOST', 'localhost')
    .factory('URL', function(HOST) { return "http://" + HOST })
    

    If you need access to you constants in providers, then I guess you have to do some more work:

    .constants('HOST', 'localhost')
    .provider('DOMAIN', function(HOST) {
        var domain = "http://" + HOST;
        this.value = function() { return domain };
        this.$get = this.value;
     })
     .provider("anyOtherProvider", function(DOMAINPovider) {
         var domain = DOMAINProvider.value();
     };
     .factory("anyOtherService", function(DOMAIN) {
     })
    

提交回复
热议问题