ViewChildの書き方

ts

export class AppComponent {
  @ViewChild('v1') protected countComponent: CountComponent;
  @ViewChild('v2') protected countComponent2: CountComponent;
}

html

  <div>
    <count #v1 [c]="100"></count>
    <button (click)="onClick1()">+</button>
  </div>
  <div>
    <count #v2 [c]="1"></count>
    <button (click)="onClick2()">+</button>
  </div>

.ts ファイルの 'v1' と .htmlファイルの #v1 で 同じコンポーネントが複数ある場合に識別します。

引用 : https://angular.keicode.com/basics/component-interaction-viewchild.php

No.1903
11/18 17:23

edit