Create dynamic scope variables in AngularJs inside loop

Vaibhav Magon

I am new to Angular.js and am trying to create dynamic scope variables in AngularJs inside a for Loop. This is something as below:

$scope.lists=[{listName:'list1'},{listName:'list2'}];

for(var i=0;i<$scope.lists.length;i++){
  var listName = $scope.lists[i].listName;
  listName = $parse(listName);
  listName.assign($scope,[]);
  $scope.$apply();
}

The above code throws an error saying: $digest already in progress.

The code works ok when used without looping just for one as done in: Setting dynamic scope variables in AngularJs - scope.<some_string>

I ultimately am looking for $scope.list1=[] and $scope.list2=[] as 2 separate arrays.

Any leads would be awesome. Thanks.

mohamedrias

The above code throws an error saying: $digest already in progress.

You're already in the controller and in angular scope. So no need to trigger the digest loop using $scope.$apply(). Even if you have to must check the $$phase and then apply.

if (!$scope.$$phase) $scope.$apply()

But for your scenario, it's not required at all

$scope.lists = [{listName: 'list1'}, {listName: 'list2'}];

angular.forEach($scope.lists, function(item) {
    var listName = item.listName;
    $scope[listName] = [];
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to add Dynamic Scope variables to a for loop in angularjs?

From Dev

Changing AngularJS scope variables from an inside function

From Dev

Scope of Variables: Instantiation Inside or Outside Loop

From Dev

Keeping variables inside the scope of the loop where they are used

From Dev

Keeping variables inside the scope of the loop where they are used

From Java

Setting dynamic scope variables in AngularJs - scope.<some_string>

From Dev

Angularjs: Create a new scope property inside a directive

From Dev

Dynamic created variables inside for loop, Javascript

From Dev

Create variables dynamically inside loop in python

From Dev

Shell : create and assign variables inside for loop

From Dev

Create variables inside an each loop in a pug template

From Dev

Dynamic templates and scope variables

From Dev

Scope inside the scope. AngularJS

From Dev

Scope inside the scope. AngularJS

From Dev

AngularJS - Dynamic scope fields

From Dev

Dynamic scope variable in angularjs

From Dev

Filtering AngularJS inside the scope

From Dev

AngularJs scope variables in console

From Dev

Testing AngularJS scope variables

From Dev

Javascript Set Dynamic Variables inside a For Loop Within addEventListener

From Dev

Scope of variable inside for loop

From Dev

Create an Object inside a loop with a dynamic key name storing another object

From Dev

how to create dynamic variable name inside a for loop in sql

From Dev

create $scope while loop

From Dev

AngularJS dynamic scope name in HTML

From Dev

Angularjs accessing scope for dynamic elements

From Dev

Fill angularjs scope with a dynamic array

From Dev

AngularJS: accessing scope variables in $routeProvider

From Dev

Angularjs $scope variables not updating in view

Related Related

  1. 1

    How to add Dynamic Scope variables to a for loop in angularjs?

  2. 2

    Changing AngularJS scope variables from an inside function

  3. 3

    Scope of Variables: Instantiation Inside or Outside Loop

  4. 4

    Keeping variables inside the scope of the loop where they are used

  5. 5

    Keeping variables inside the scope of the loop where they are used

  6. 6

    Setting dynamic scope variables in AngularJs - scope.<some_string>

  7. 7

    Angularjs: Create a new scope property inside a directive

  8. 8

    Dynamic created variables inside for loop, Javascript

  9. 9

    Create variables dynamically inside loop in python

  10. 10

    Shell : create and assign variables inside for loop

  11. 11

    Create variables inside an each loop in a pug template

  12. 12

    Dynamic templates and scope variables

  13. 13

    Scope inside the scope. AngularJS

  14. 14

    Scope inside the scope. AngularJS

  15. 15

    AngularJS - Dynamic scope fields

  16. 16

    Dynamic scope variable in angularjs

  17. 17

    Filtering AngularJS inside the scope

  18. 18

    AngularJs scope variables in console

  19. 19

    Testing AngularJS scope variables

  20. 20

    Javascript Set Dynamic Variables inside a For Loop Within addEventListener

  21. 21

    Scope of variable inside for loop

  22. 22

    Create an Object inside a loop with a dynamic key name storing another object

  23. 23

    how to create dynamic variable name inside a for loop in sql

  24. 24

    create $scope while loop

  25. 25

    AngularJS dynamic scope name in HTML

  26. 26

    Angularjs accessing scope for dynamic elements

  27. 27

    Fill angularjs scope with a dynamic array

  28. 28

    AngularJS: accessing scope variables in $routeProvider

  29. 29

    Angularjs $scope variables not updating in view

HotTag

Archive