Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS : IF Statement inside "ng-repeat"?

I'm using AngularJS v1.2.9.

Inside ng-repeat, how do i use check the value of object and do action based on condition?

In English, something like:

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

Here's 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 null. If it is null, then i say something else.

Please kindly help. Thanks all.

like image 635
夏期劇場 Avatar asked Mar 29 '16 11:03

夏期劇場


People also ask

How do I remove duplicates in NG-repeat?

You can use unique filter while using ng-repeat . If you use track by $index then unique won't work. ok, I used unique and its working now, thanks!

What can I use instead of NG-repeat?

And you should consider using ng-repeat with pagination. You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

How do you pass an index in NG-repeat?

Each ng-repeat creates a child scope with the passed data, and also adds an additional $index variable in that scope. So what you need to do is reach up to the parent scope, and use that $index . Save this answer.

What is Ng-repeat in AngularJS?

AngularJS ng-repeat Directive The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.


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

like image 97
JustOneMoreQuestion Avatar answered Nov 09 '22 13:11

JustOneMoreQuestion


Update

Question wasn't much clear so here is what you wanted just by having ng-bind directive in place. If the content is bigger then you can switch to the way 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 could easily do by 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 which will filter out those will create a collection of element which has date in it.

<li ng-repeat="r in results| filter: { date: '' }">
    <!--
    if ( r.date!=="null" ) {
        {{ r.date }}
    }
    -->
</li>
like image 44
Pankaj Parkar Avatar answered Nov 09 '22 13:11

Pankaj Parkar


Check this one,

<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>
like image 31
Anil Kumar Ram Avatar answered Nov 09 '22 13:11

Anil Kumar Ram