3

I want to create dynamic scope variables. Here is a sample for loop that I created

for(var i=0; i<array.length; i++){ $scope.'linkName'+i = 'Some value'; // it should be like $scope.linkName1, $scope.linkName2, etc. }

The problem is that it doesn't allow for me to create it this way. Do you know the best way that I can use a for loop to loop through an array of variable names and assign that value to the scope variable?

1 Answer 1

7

Try this

$scope['linkName'+i] = 'Some value';

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

2 Comments

Thanks so much! Worked like a charm!
how to render the value of the dynamic variable inside the template? it is not working for me . I tried to use like this $scope["variableName_" + i] = 1122; {{"variableName_"+ $index}} , In rendered UI, it showed variable name instead of value in it. UI-Result: variableName_0 variableName_1 ..etc

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.