Maria Jane Maria Jane - 1 month ago 7
AngularJS Question

ng-repeat without declaring array of object

<div ng-init="num_cover_photo:new Array(4)">
<div ng-repeat="photo in num_cover_photo">
<img ng-src="cover_"{{$index}} />
</div>
</div>


I know above code won't work, but that's my idea. I don't want to declare something like this

$scope.cover_photo = [{
bla
bla
}]


because what I want is simple, able to use ng-repeat to build few block of markup. I'd google but many suggested I create a dumb array of object in controller. Any clue I can ng-init it on the fly?

Answer

If all you want to do is create a few blocks of markup, you could do a simple for loop in the ng-repeat

like so:

<div data-ng-repeat="i in [1,2,3,4,5]">
   do something
</div>

to create 5 blocks.

Comments