Call a component method from HTML in Angular2

2019-04-04 15:27发布

Is it possible to call a component method from HTML, or should I create another component to handle formatting?

<div *ngFor="let item of items">
  <div class="title">{{ item.Title }}</div>
  <p>
    callComponentMethodHere({{item}})
  </p>
</div>

1条回答
小情绪 Triste *
2楼-- · 2019-04-04 15:56
{{callComponentMethodHere(item)}}

but you should avoid that because the method will be called every time change detection runs. It's better to call the method in code (for example in the constructor(), ngOnInit(), or an event handler, assign the result to a property and from the view bind to that property instead.

Calling event handlers is fine of course:

<button (click)="callComponentMethodHere(item)">click me</button>
查看更多
登录 后发表回答