0
<mat-cell *matCellDef="let element; let i index">{{ element.patientName }}<input type="hidden" [(ngModel)]="index" value={{i}}/></mat-cell>

I made an index variable to use the value in the type script and tried console.log(index) and found undefined.

export class DashboardComponent implements OnInit, OnDestroy {

index: number;

constructor(){}

console.log(index);
}
2
  • to use template value in component you need to trigger it via click event and pass index in click event method. Commented Jul 28, 2020 at 8:03
  • Thank you. I understood what you said after seeing the answer. Commented Aug 19, 2020 at 2:27

1 Answer 1

1

let i index change it to index as i . Doc here

https://angular.io/api/common/NgForOf

If you need to use it inside .ts . Write the line below inside ngfor. It will put each i to store function when you click button [ write a store function in your typescript before ]

<button (click)="store(i)"> </button>

Your project might not need button , use anything you want, but just see you need (event) to pass value in typescript. Events can be : (click) / (change) / (use any event you need)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, but if the index value is good, I would like to use this value in the component. I'd like to know how to use the value value in the component. If I use ngmodel, it comes out undefined.

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.