본문 바로가기

Angular.js

*ngFor , *ngIf , (click)="func(Hero)"

아래와 같이 html에서 사용할 selectedHero변수,  오클릭메소드 등을 ts파일에 설정해준다.

---------app.component.ts-----------

export class HeroesComponent implements OnInit {
  hero = {
    id: 1,
    name: 'kimtae',
  };
  heroes = HEROES;
  selectedHero: Hero;

  onSelect(hero) {
    this.selectedHero = hero;
  }

  constructor() {}

  ngOnInit(): void {}
}

html을 아래와 같이 작성한다.

--------heores.component.html----------------

//ngFor

<ul>
  <li *ngFor="let hero of heroes" (click)="onSelect(hero)">
    {{ hero.id }}
    {{ hero.name }}
  </li>
</ul>


//ngIf
<div *ngIf="selectedHero">
  <p>선택된 히어로 :</p>

  {{ selectedHero.name }} </span>

</div>

'Angular.js' 카테고리의 다른 글

Angular Service [1]  (0) 2020.07.06
컴포넌트 분리 ( 컴포넌트간 통신 @input )  (0) 2020.07.06
ts 파일에서 : 와 = 차이점  (0) 2020.07.06
새로운 컴포넌트 등록 및 데이터 바인딩  (0) 2020.07.06
데이터 바인딩  (0) 2020.07.06