0

I want to know if it is possible to use another value defining value in Angular? Let's say I have two defined values and want to define third value using first two:

angular.module('my.module').value('firstOne', ['John', 'Mary']);
angular.module('my.module').value('secondOne', ['Tom', 'Jack']);

angular.module('my.module').value('combined', [firstOne[0], secondOne[1]);
3
  • As there is no way to inject anything into a value I'm guessing the answer is no. Commented Mar 22, 2016 at 14:04
  • 1
    It is possible to use a "value" to define another "value". Also, if you want to change it within your code at some point, use value, if not, use constant. Follow the explanation: ilikekillnerds.com/2014/11/… Commented Mar 22, 2016 at 14:09
  • @klauskpm can you write example of it in an answer? Commented Mar 22, 2016 at 14:12

2 Answers 2

2

you can do this,

var app = angular.module('my.module', []); //just a shortcut, don't have to do this

app.value('firstOne', ['John', 'Mary']);
app.value('secondOne', ['Tom', 'Jack']);

app.service('constantService', function(firstOne, secondOne) {
  this.combined = [firstOne[0], secondOne[1]];
});

then use it anywhere as inject constantService in the function, and then call constantService.combined

Sign up to request clarification or add additional context in comments.

2 Comments

That is pretty much what I have been thinking, if there will be no other answers I will accept this one
He can also declare combined as app.value('combined', []); call it on function and finish like combined.push([firstOne[0], secondOne[1]);
0

we can done your task by doing below code.

angular.module('my.module')
.value('RESOURCES', (function() {
 var firstOne = ['John', 'Mary'];
 var secondOne = ['Tom', 'Jack'];
 return {
   firstOne: firstOne,
   secondOne: firstOne,
   combined: firstOne[0] + firstOne[1]
}
})())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.