0

** Good day house, Please I am having difficulties trying to use *ngFor to retrieve first and last objects of an array**

<ion-item *ngFor="let payment of allPaymentGateways; let first = first; let last = last "              [ngClass]="{first: first, last: last}">
                <ion-label>
                  <ion-row class="full" class="align-items-center">
                    <ion-col size="10">
                      <ion-row class="align-items-center">
                        <p class="no-margin">
                          <strong>{{ payment.method_title }}</strong>
                        </p>
                      </ion-row>
                      <ion-row class="align-items-center">
                        <p>{{ payment.description }}</p>
                      </ion-row>
                    </ion-col>
                  </ion-row>
                </ion-label>
                <ion-radio slot="start" (click)="choosePaymnetGateway(payment)"></ion-radio>
              </ion-item>
3
  • What exactly is the problem here? Are you having trouble applying CSS based on the first and last objects? Commented Mar 14, 2020 at 20:06
  • I want to display the first element and the last element in the list eg.const HEROES = [ {id: 1, name:'Superman'}, {id: 2, name:'Batman'}, {id: 5, name:'BatGirl'}, {id: 3, name:'Robin'}, {id: 4, name:'Flash'} ]; Commented Mar 14, 2020 at 20:46
  • Please see my answer. Commented Mar 14, 2020 at 21:06

1 Answer 1

2

To display only first and last items in the array, use *ngIf directive. Try the following

<ion-item *ngFor="let payment of allPaymentGateways; let first=first; let last=last" [ngClass]="{first: first, last: last}">
  <ng-container *ngIf="first || last">
    <ion-label>
      <ion-row class="full" class="align-items-center">
        <ion-col size="10">
          <ion-row class="align-items-center">
            <p class="no-margin">
              <strong>{{ payment.method_title }}</strong>
            </p>
          </ion-row>
          <ion-row class="align-items-center">
            <p>{{ payment.description }}</p>
          </ion-row>
        </ion-col>
      </ion-row>
    </ion-label>
    <ion-radio slot="start" (click)="choosePaymnetGateway(payment)"></ion-radio>
  </ng-container>
</ion-item>
Sign up to request clarification or add additional context in comments.

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.