问题
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:
{{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>
来源:https://stackoverflow.com/questions/42066490/call-a-component-method-from-html-in-angular2