简体   繁体   中英

Call a component method from HTML in Angular2

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>
{{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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM