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)"
1
  • 2
    don't use ng-init for such initialization..keep this is controller would make more sense.. Commented Jan 28, 2016 at 12:20

4 Answers 4

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

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

Comments

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>

Comments

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 = [];

Comments

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

Comments

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.