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
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]"