AngularJS: IF Statement inside "ng-repeat"?

I am using AngularJS v1.2.9 .

Inside ng-repeat , how can I check the value of an object and execute an action based on a condition?

In English, something like:

 if `X` is not null, then show `X`. Is it possible? 

Here is my ng-repeat skeleton:

 <li ng-repeat="r in results"> <div>{{ r.title }}</div> <div>{{ r.location }}</div> <!-- if ( r.date!=="null" ) { <div>{{ r.date }}</div> } else { <div>Date not confirmed yet.</div> } --> </li> 

I need to show r.date if it is not empty. If it is zero, then I say something else.

Please, kindly help. Thanks to everyone.

+5
source share
3 answers
 <li ng-repeat="r in results"> <!-- If r.date contains a value, show it --> <span ng-if="r.date">{{r.date}}</span> <!-- If r.date does not contain a value show placeholder text --> <span ng-if="!r.date">Date not confirmed yet.<span> </li> 

Also see What does an exclamation mark before a variable mean in JavaScript

+8
source

Update

The question was not very clear, so here is what you need only with the ng-bind directive. If the content is larger, you can switch to the method suggested by @JustOneMoreQuestion below.

 <li ng-repeat="r in results" ng-if="r.date"> <div ng-bind="r.date ? r.date : 'Date not confirmed yet.'"></div> </li> 

You can easily do using ng-if

 <li ng-repeat="r in results" ng-if="r.date"> <!-- if ( r.date!=="null" ) { {{ r.date }} } --> </li> 

OR go for a filter that will filter them, create a collection of the element with date in it.

 <li ng-repeat="r in results| filter: { date: '' }"> <!-- if ( r.date!=="null" ) { {{ r.date }} } --> </li> 
+3
source

Check this,

 <li ng-repeat="r in results" > <div data-ng-if="r.date !== 'null'"> Your Date </div> <div data-ng-if="r.date == 'null'"> Date not confirmed yet </div> </li> 
+1
source

Source: https://habr.com/ru/post/1245977/


All Articles