2

Is it possible to create an array using ng-init dynamically? The bellow code is not working

 ng-init="medi_count = new Array(5)"
Shijin TR
  • 7,516
  • 10
  • 55
  • 122
  • 2
    don't use [`ng-init`](https://docs.angularjs.org/api/ng/directive/ngInit) for such initialization..keep this is controller would make more sense.. – Pankaj Parkar Jan 28 '16 at 12:20

4 Answers4

7

Angular expression is not same like JavaScript expression. It has some limits.!

No Object Creation With New Operator: You cannot use new operator in an Angular expression.

Refer Angular Documentation : Angular Expression

codeninja.sj
  • 3,452
  • 1
  • 20
  • 37
5

Sure you can its just the same way you create Arrays..

    <!DOCTYPE html>
    <html ng-app>
    <head>
    <script src="https://code.angularjs.org/1.4.9/angular.js"></script>
    </head>
    <body>
        <div ng-init="names=[{name:'Shijin',city:'NewYork'},   {name:'John',city:'Montana'},{name:'Phillip',city:'California'}]">
            <ul>
                  <font face="Impact" color="purple" size = "5"><li data-ng-repeat="Objects in names">{{Objects.name}} - {{Objects.city}}</li></font>
            </ul>
        </div>
    </body>
    </html>
amanuel2
  • 4,508
  • 4
  • 36
  • 67
0

ng-init is for loading data into an array, as other answer shows. If you just want a new empty array, then make it in the controller

 $scope.medi_count = [];
Simon H
  • 20,332
  • 14
  • 71
  • 128
0

You can do it pretty fine by calling a function

<div ng-init="medicount(5)">

where

$scope.medicount = function(someNumber){
   //what you want
}

as answered in this question

Community
  • 1
  • 1
Asqan
  • 4,319
  • 11
  • 61
  • 100