2

If I declare 1 constant like this:

app.constant('appHelper', { first: 'Firstname', last: 'Lastname' }); 

and then try to use it in a second constant like this:

app.constant('appHelper2', { fullName: appHelper.first + '' + appHelper.last }); 

doesn't work. Error: appHelper is undefined.

Any ideas how can I do that ?

1

2 Answers 2

1

Sure:

var appHelper = { first: 'Firstname', last: 'Lastname' } app.constant('appHelper', appHelper); app.constant('appHelper2', { fullName: appHelper.first + '' + appHelper.last }); 
Sign up to request clarification or add additional context in comments.

Comments

0

Reference and Credits:

is there a way in Angularjs to define constants with other constants?

http://michalostruszka.pl/blog/2012/12/23/angular-better-constant-values/

Based on this, the following may be suitable for you.

var myApp = angular.module("exampleApp",[]); myApp.constant('HELPER', (function() { // Define your variable var appHelper = { first: 'Firstname', last: 'Lastname' }; // Use the variable in your constants return { appHelper:appHelper, fullName: appHelper.first + '' + appHelper.last } })()); myApp.controller("ExampleCtrl", function(HELPER){ $scope.firstname = HELPER.appHelper.firstname; $scope.fullName = HELPER.fullName; }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.