0
<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?

2
  • Is there a good reason you don't want to declare a simple scope object? It's making this harder on you if you don't. Commented Oct 26, 2016 at 14:37
  • What do you want to iterate if you don't have any collection? How do you imagine it working? Commented Oct 26, 2016 at 14:41

3 Answers 3

1

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.

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

Comments

0

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>

Comments

0

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);   
}

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.