ng-init with ng-repeat not working

I am trying to display some numbers in a div using as like below

 <div ng-init="range=[1,10,3,4,5]" ng-repeat="n in range" >
  {{n}}
 </div> 

It is not showing the numbers in range

Answers


Initialize before ng-repeat

<div ng-init="range=[1,10,3,4,5]" >
<div ng-repeat="n in range" >
  {{n}}
 </div> 
</div>

Fiddle for solution:https://jsfiddle.net/ggbnanoL/


Initialize the values before ng-repeat

<div ng-init="range=[1,10,3,4,5]"></div>
<div ng-repeat="n in range" >
    {{n}}
</div>

Plunker


ng-init should only be used for aliasing: https://docs.angularjs.org/api/ng/directive/ngInit

Can you not initialise the range value in your controller?


Try this one

<!DOCTYPE html>
<html ng-app>
<head>
    <title>AngularJS</title>
    <script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>

</head>
<body>
    <ANY ng-init="range=[1,10,3,4,5]">
    </ANY>
    <div ng-repeat="n in range">
    {{n}}
    </div>
</body>
</html>

Need Your Help

Moment.js - how do I get the number of years since a date, not rounded up?

javascript date momentjs

I'm trying to calculate a person's age using Moment.js, but I'm finding that the otherwise useful fromNow method rounds up the years. For instance, if today is 12/27/2012 and the person's birth dat...

Converting XElement to HTML string

html xml linq scripting xelement

I am performing XML Operations on an XHTML document utilizing Xml and Linq.