angularjsangular-materialscopengfor

Want to use a variable outside the scope of ngFor


<mat-select [(ngModel)]="e1Instance" (selectionChange)="e1InstanceChanged(item)"> <mat-option *ngFor="let item of entities" value={{item.name}} >{{item.name}}</mat-option></mat-select> Hi Community, This is how I want to use the code... I want to use the item variable on first line in (SELECTIONCHANGE) If i move my ngFor above it is going to create a chaos making multiple mat-selects. Any Suggestions

I want to use the item variable on first line in (SELECTIONCHANGE) If i move my ngFor above it is going to create a chaos making multiple mat-selects. Any Suggestions


Solution

  • Here is a solution that I found:

    • Create a new function of onClick on Mat-Option.
    • Create that function in your TS file.
    • Save the value of current entity in some other variable.
    • Pass that variable in SELECTIONCHANGE.