Maria Jane
Maria Jane

Reputation: 2403

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?

Upvotes: 0

Views: 362

Answers (3)

Max Taylor
Max Taylor

Reputation: 343

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.

Upvotes: 1

Eren Akkus
Eren Akkus

Reputation: 471

maybe it helps you;

<div ng-repeat="i in Num(4) track by $index">
    <img ng-src="cover_"{{$index}} />
</div>

$scope.Num = function(num) {
    return new Array(num);   
}

Upvotes: 0

brad
brad

Reputation: 559

Might I suggest initializing the array with the numbers you want to use and then referencing them in the ng-repeat instead of using $index

<div ng-init="num_cover_photo=[1, 2, 3, 4]">
    <div ng-repeat="photo in num_cover_photo">
        <img ng-src="cover_{{photo}}" />
    </div>
</div>

Upvotes: 0

Related Questions