Sarfraz Ahmad Sarfraz Ahmad - 1 year ago 69
AngularJS Question

Is there any way concatenate the variable name with a string or it is basic programming Mistake

I am working on an AngularJs app. I have an array of HTML select tags where I am concatenating the ng-model name and the options list name with a variable. He re I have two list or arrays. the first array is a levels list which is used to append multiple select tags on the document.

<div data-ng-repeat="level in levelslist">
<select ng-model="subtopics_"+{[{level}]} ng-options=" for subtopic in subtopicslist_"{[{level}]} ng-change="loadOtherSubTopics(level)">

as seeing in the HTML code, i am concatenating the level with the array name and the model name. and calling a function on ng-change. which is an AJAX request.

my AngularJs functions is

success(function(data, status) {
error(function(data, status) {
$ = data || "Request failed";
$scope.status = status;

i have a level variable in the data return by the backend. what i want to do is as i have lists and ng-models names concatenated with the level variable. while assigning the data to that list, i want to concatenate the list name with the data.level recieved from the ajax request as


but when i m trying to do it , there is an error that

ReferenceError: invalid assignment left-hand side

and i am trying to concatenate the name of the list or array . Tell me how can i do this.
like as in python we can do this using %s %(level). how can we apply same in AngularJs?
help will be appreciated

Answer Source

Although possible, I suspect concatenating variable names isn't what you need here. You can use an object to achieve what you need. In the controller you can define it to be an object:

$scope.subtopicslist = {};

And then on $http success, you can assign properties by "level"

$scope.subtopicslist[data.level] = data.subtopicslist;

And then in the template you can still use a single element of this object as a model

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download