0

I am using Angular 4+. Please note: first and last needs to be based on the class instance, not the element. So a let first = first in the ngFor will not work. I currently imported jQuery to handle this but would like to see if there is a way not to use jQ.

In the component ts file:

items: any = [{number: 0, text: 'blah blah'},{number: 0, text: 'blah blah'},{number: 1, text: 'blah blah'},{number: 1, text: 'blah blah'},{number: 1, text: 'blah blah'}, etc...

In the HTML file:

<ion-row *ngFor="let item of items [ngClass]="'item-class-' + item.number">
  <ion-col>{{item.text}}</ion-col>
</ion-row>

Now what I want to do is grab the first instance of a class and last - once I have it I want to style it or add a class to the row. For example in jQuery I would have done this:

$( 'ion-row.item-class-0' ).first().css( 'background-color', 'red' );
$( 'ion-row.item-class-1' ).first().css( 'background-color', 'red' );
RooksStrife
  • 1,647
  • 3
  • 22
  • 54

1 Answers1

0

updated answer :

template :

<ion-row *ngFor="let item of items" [ngClass]="'item-class-' + item.number">
    <ion-col>{{item.text}}</ion-col>
  </ion-row>

component.ts

import { Component, AfterViewInit } from '@angular/core';
import * as $ from 'jquery';

@Component({...})
export class HomePage {

    items: any = [{
        number: 0,
        text: 'blah blah'
    }, {
        number: 0,
        text: 'blah blah'
    }, {
        number: 1,
        text: 'blah blah'
    }, {
        number: 1,
        text: 'blah blah'
    }, {
        number: 1,
        text: 'blah blah'
    }];

    ngAfterViewInit() {
        $( 'ion-row.item-class-0').first().css( 'background-color', 'red' );
        $( 'ion-row.item-class-1').last().css( 'background-color', 'red' );
    }
}

enter image description here

it will be working for me try this :

Chandru
  • 10,864
  • 6
  • 38
  • 53