0

I currently have an Angular component being replaced for each instance of an array:

<app-doc-item
    *ngFor="let docFeatureEl of docFeatures; let i = index";
    [documentFeature]="docFeatureEl"
    ></app-doc-item
  >

I would like to access the index each index (i) and print that value from within the component:

My child component looks as follows:

<a href="#" class="list-item" (click)="onSelected()">
  <div class="pull-left">
    <h4 class="list-group-item-heading">{{ docFeature?.string }}</h4>
  </div>
</a>

ideally, the goal is to pass that index as a parameter for the onSelected() to perform actions on that particular instance,

unfortunately whenever I try to access i Angular tells me property 'i' does not exist on type

How do I access this index produced from within the component?

Cheers!

1
  • I am not well versed in angularJS, but in modern angular I would pass that index as an input. Maybe you could try that Commented Feb 11, 2023 at 19:29

1 Answer 1

1

Add index as an input on app-doc-item component

<app-doc-item
    *ngFor="let docFeatureEl of docFeatures; let i = index";
    [documentFeature]="docFeatureEl"
    [index]="i">
</app-doc-item>

And use it in your onSelected

<a href="#" class="list-item" (click)="onSelected(index)">
  <div class="pull-left">
    <h4 class="list-group-item-heading">{{ docFeature?.string }}</h4>
  </div>
</a>
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.