2

I'm using an ngFor to list my data, and would like to be able to click an element and have it call a method in my component class, identifying which element of the data it is. My attempts basically came to something like this:

<ng-template ngFor let-item [ngForOf]="data">
  <span class="table-select" (click)={{"editData(" + item + ")"}}>{{item}}</span>
</ng-template>

However that doesn't work and throws this error:

Error: Template parse errors: Unexpected closing tag "span".

I have thought about just passing this and using the native element to figure out which element it is, but that feels dirty and not very angular y. So am I doing this wrong? Or is there a better way to go about this?

Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73
Incinirate
  • 114
  • 2
  • 11

1 Answers1

1

try

<span class="table-select" (click)="editData(item)">{{item}}</span>

angular will know to look in your component class for the editData method because of the directive (click)

LLai
  • 13,128
  • 3
  • 41
  • 45
  • 2
    Oh wow ok, so the interpolation isn't needed, those variables are injected into the scope when that expression gets called, good to know! Thanks! – Incinirate Jun 20 '17 at 15:42