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

前端 未结 6 664
刺人心
刺人心 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:48

    An easy way to do this is like this:

    var myApp = angular.module("exampleApp",[]);
    
    myApp.constant('RESOURCES', (function() {
      // Define your variable
      var resource = 'http://127.0.0.1:8008';
      // Use the variable in your constants
      return {
        USERS_DOMAIN: resource,
        USERS_API: resource + '/users',
        BASIC_INFO: resource + '/api/info'
      }
    })());
    

    And use the constants like this:

    myApp.controller("ExampleCtrl", function(RESOURCES){
      $scope.domain = RESOURCES.USERS_DOMAIN;
    });
    

    Credits: link

提交回复
热议问题