16

I want the ng-repeat to run only when the photos.length > 1

Doing this doesn't seem to work:

<div ng-repeat="thumb in photos | photos.length > 1">

Is there any quick trick / elegant way to do this? I just need to check for array length and nothing else fancy.

I would hate to duplicate the array to another array by doing something like this

if (oldArray.length > 1)
 newArray = photos;
else
 newArray = [];

...and then do ng-repeat on newArray. This seems inefficient.

Thanks.

1

2 Answers 2

40

Try:

<div ng-repeat="thumb in photos" ng-if="photos.length > 1"></div>

if the ng-if condition is not satisfied those elements will be removed from the DOM. See http://docs.angularjs.org/api/ng/directive/ngIf for further reference.

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

2 Comments

It may also be helpful to check for the existence of 'photos' prior to accessing the length property since 'undefined' would not have a length property. For example: ng-if="photos && photos.length > 1"
how to have two if confition, before and after ng-repeat? for example, photos can be array or an error, if the type is string it's an error, and i have to check it before ng-repeat, but then i want to put condition on thumb, say if it meet specific condition, then i have to run it after ng-repeat...
5

You could try using the ng -if directive to check the condition you describe e.g.

<div ng-if='photos.length > 1'> your HTML here </div>

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.