Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

 ng-init="medi_count = new Array(5)"
share|improve this question
2  
don't use ng-init for such initialization..keep this is controller would make more sense.. – Pankaj Parkar Jan 28 at 12:20
up vote 4 down vote accepted

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

share|improve this answer

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>

share|improve this answer

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 = [];
share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.