0

I have declared and initialized few Scope variables and want to access them dynamically, but when I try to access a variable from html file, that variable is treated as a string.

JS Code-

$scope.abc3txt = "Hello";
$scope.abc2txt = "hello everyone";
$scope.abc1txt = "hello 123";

There is a number with every variable like 3,2,1 between abc"number"text.

I want to access the variable value according to the number in my html file.

HTML code-

<h4>{{abc+a.status+txt}}</h4>

a.status has some value like 1,2 or 3.

This prints like "abc3txt" in html, instead of returning the initialised value of "$scope.abc3txt" variable.

Please someone help..

2
  • Use an array or object! Variable variables are madness. $scope.abc[3] = { txt: 'Hello' }, or just $scope.abc[3] = 'Hello' or probably just $scope.abc = ['Hello', 'hello ...', ..]. This can be accessed unambiguously. Commented May 17, 2016 at 8:28
  • Not yet @kiro112.. Going to try this Commented May 17, 2016 at 8:29

3 Answers 3

3

I don't know if it can be done with $scope directly. A way would be to save those values in an object:

$scope.values = {
   abc3txt: "Hello",
   abc2txt: "hello everyone",
   abc1txt: "hello 123"
};

And in you view you could access them like this:

{{values['abc' + a.status + 'txt']}}

EDIT: The best alternative would be to use the controllerAs syntax. Suppose you have in controller:

var vm = this;
vm.abc3txt = "Hello";
vm.abc2txt = "hello everyone";
vm.abc1txt = "hello 123";

Then in your view: {{vm['abc' + a.status + 'txt']}}

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

4 Comments

From HTML you'll need an intermedairy field. Of you'll need to use controllerAs syntax to be able to do that.
Can we use ";" semicolon inside object, Only comma can be used right ?
Yes, to separate fields from one another in an object, you need to use comma, if you mean that with your question!
Thanks for the help @SpartakLalaj for making me understand things.. It worked!!
3

Dynamic generation of variable names in the way you are seeking is, in all possible perspective, a very, very, SUPER-VERY bad practice, and this regardless of the support (or lack of it) a language may have. Having said that, arrays is the right way to go.

Comments

3

You absolutely shouln't use variables namese that way, in javascript...
Consider using arrays...
Something like this should help:

In the controller:

$scope.a = {};
$scope.a.status = 2;
$scope.abctxt = [ "hello 123", "hello everyone", "Hello" ];

And, in the template:

<h4>{{ abctxt[a.status - 1] }}</h4>

1 Comment

Just because you used 1, 2, 3 for the names of your variables, so I supposed your array was 1-based. If it is 0-based (0,1,2,...), you can avoid -1...

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.