Events

Event hozzáadása ctrl+space-re kiadja az IDE az összes eventet:

<div>
  <button (click)="onSelectUser()">
    <img [src]="imagePath"
         [alt]="selectedUser.name"
    />
    <span>{{ selectedUser.name }}</span>
  </button>
</div>
export class UserComponent {
  selectedUser = DUMMY_USERS[randomIndex];

  get imagePath() {
    return 'assets/users/' + this.selectedUser.avatar
  }

  onSelectUser() {
    console.log("clicked");
  }
}

 

State: az event altal hivott metódust is átírhatjuk, igy dinamikusabb eredmenyt kapunk:

export class UserComponent {
  selectedUser = DUMMY_USERS[randomIndex];

  get imagePath() {
    return 'assets/users/' + this.selectedUser.avatar
  }

  onSelectUser() {
    const randomIndex = Math.floor(Math.random() * DUMMY_USERS.length)
    this.selectedUser = DUMMY_USERS[randomIndex];
  }
}