1

I have the following object binded to the $scope of my Angularjs application

{name: 'Vampire Cafe', rating: 4, review: "Food was good, cafe was a bit dark..."}

I'd like to take the value of the rating and repeat a number of icon elements on my DOM totaling that value

So given the object above, the element would repeat 4 times, and the DOM would look like so:

<h3>Rating</h3>
<i class="icon-ios-star"></i>
<i class="icon-ios-star"></i>
<i class="icon-ios-star"></i>
<i class="icon-ios-star"></i>

How can I do this with a simple Angularjs expression?

1 Answer 1

3

At the moment, ng-repeat only accepts a collection as a parameter, but you could do this:

<ul>
    <li ng-repeat="i in getNumber(number) track by $index"><span>{{$index+1}}</span></li>
</ul>

And somewhere in your controller:

$scope.number = 5;
$scope.getNumber = function(num) {
    return new Array(num);   
}

This would allow you to change $scope.number to any number as you please and still maintain the binding you're looking for.

Here is a fiddle with a couple of lists using the same getNumber function.

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

1 Comment

Very clean solution!

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.