Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 require it. I want to be sure if this isn't possible at all.

Currently I have constants in this way:

angular.module('mainApp.config', [])     .constant('RESOURCE_USERS_DOMAIN', 'http://127.0.0.1:8008')     .constant('RESOURCE_USERS_API', 'http://127.0.0.1:8008/users')     // Specific routes for API     .constant('API_BASIC_INFORMATION', RESOURCE_USERS_API + '/api/info')     .constant('API_SOCIAL_NETWORKS', RESOURCE_USERS_API + '/api/social')     ; 

The second two constants are what I want to accomplish.

like image 324
raulricardo21 Avatar asked Aug 28 '13 17:08

raulricardo21


People also ask

How do you declare a constant variable in AngularJS?

Declaring a const Variable We can use const to declare a variable but unlike let and var it must be immediately initialised, with a value that can't be changed afterwards.

How would you use constants in a script?

A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.

How to define a constant in html?

If a variable should have a fixed value that cannot be changed, you can use the const keyword. The const keyword declares the variable as "constant", which means that it is unchangeable and read-only.

How constant can be defined?

Constants can be defined using the const keyword, or by using the define()-function. While define() allows a constant to be defined to an arbitrary expression, the const keyword has restrictions as outlined in the next paragraph. Once a constant is defined, it can never be changed or undefined.


2 Answers

The AngularJS way to define dependencies between Controllers, Services and others is by dependency injection (DI). So if you have a controller A that depends on a service B you would have to create it like this:

var myApp = angular.module("exampleApp",[]);  myApp.controller("aCtrl", function(serviceB){     // Controller functionally here }); 

See, AngularJS will check the serviceB dependency and look for the service you created with that name. If you don't create one you will get an error.

So, if you want to create a constant A that depends on constant B, you would need to tell angular that A depends on B. But a constant can't have a dependency. A constant can return a function, but the DI won't work for the constant. Check this Fiddle so you can see for which methods DI work for.

So answering your question, you can't define a constant with other constants.

But you can do this:

angular.module('projectApp', [])   .constant('domain', 'http://somedomain.com')   .constant('api', '/some/api/info')   .service('urls', function(domain, api) {this.apiUrl = domain + api;})    .controller('mainCtrl',function($scope,urls) {        $scope.url = urls.apiUrl;    }); 

Check this fiddle to see it working:

If you want to understand more about DI, check out this post.

like image 22
lao Avatar answered Sep 21 '22 18:09

lao


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

like image 94
Linkmichiel Avatar answered Sep 18 '22 18:09

Linkmichiel