Angular2事件绑定中的字符串插值

时间:2017-06-20 15:34:52

标签: angular string-interpolation angular2-databinding

我正在使用ngFor列出我的数据,并且希望能够单击一个元素并让它调用我的组件类中的方法,确定它是哪个数据元素。我的尝试基本上是这样的:

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

然而,这不起作用并抛出此错误:

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

我考虑过只传递this并使用原生元素来确定它是哪个元素,但感觉很脏而且不是很 angular y。我这样做错了吗?或者有更好的方法来解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

<span class="table-select" (click)="editData(item)">{{item}}</span>
由于指令(点击)

angular将知道在您的组件类中查找editData方法