Is there any way concatenate the variable name with a string or it is basic programming Mistake
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
ng-model="subtopicslist[level]"
Comments
-
Sarfraz Ahmad almost 2 years
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="subtopic.name for subtopic in subtopicslist_"{[{level}]} ng-change="loadOtherSubTopics(level)"> </select> </div>
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$scope.loadSubTopics=function(){ $http.get('/pathfortherequest/). success(function(data, status) { $scope.subtopicslist_+data.level=data.subtopicslist; }) . error(function(data, status) { $scope.data = 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
$scope.subtopicslist_+data.level=data.subtopicslist;
but when i m trying to do it , there is an error that
ReferenceError: invalid assignment left-hand side $scope.subtopicslist_+data.level=data.subtopicslist;
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
-
Sarfraz Ahmad over 10 yearsthanx , but what i did is i concatenate the array name with the level value. there are multiple subtopics list which has their own subtopics. I am doing this in a recursive way
-
Michal Charemza over 10 yearsI don't think what I posted means you can't do that. The "level" variable can be any string. Say, "home_office_chairs". Although I would say it's usual to use multidimensional arrays/objects for this, like subtopicslist["home"]["office"]["chairs"].
-
Sarfraz Ahmad over 10 yearsYes, I am Trying this. the same solution you told me, thanx
-
Sarfraz Ahmad over 10 yearsYeah, It works for me. thankyou. now in HTML i used <div data-ng-repeat="level in levelslist"> <select ng-model="subtopics_"+{[{level}]} ng-options="subtopic.name for subtopic in subtopicslist[{[{level}]}]" ng-change= "loadOtherSubTopics(level)" > </select> </div> means just assing the index to the subtopicslist and changed the js code as u suggested. Thank you
-
Michal Charemza over 10 yearsIt surprises me that ng-model="subtopics_"+{[{level}]} works as you expect to be honest, as I would think everything after the closing " would be ignored by the ngModel directive.
-
Sarfraz Ahmad over 10 yearsThat ng-model i didn't check. I checked till the list. means the values a assigned to the list has been assigned. and i think there may be same problem with ng-model because concatenation with the variables.
-
Sarfraz Ahmad over 10 yearsYes, as u said and i expected, the same problem. Cannot concatinate variables .
-
Sarfraz Ahmad over 10 yearsI did this by giving name ng-model="subtopics" and then passing the complete element to the function on ng-change=loadmoresubtopics(subtopics). this has solved my problem. but i assigned the array index to the ng-model it produces a syntex error