Ramki
Ramki

Reputation: 381

Get component dynamically in method and set its property in angular

I am a developing an ionic game, How can i get the properties of a child component(app-square) in a method in the parent component(app-board). app-board as over 30 app-square components, so i want it to be able to dynamically access it board-component.html(parent)

<app-square (click)="makeMove(l1)" [count]="3" #l1></app-square>
  <app-square #l2></app-square>

board-component.ts

makeMove(obj: SquareComponent){
//I want to change the value of l2 here
    }

I understand i cannot use ViewChild as it doesn't work inside methods.

Upvotes: 0

Views: 149

Answers (1)

onik
onik

Reputation: 1631

reference it using ViewChildren decorator inside board component like this

@ViewChildren(QuareComponent)
squareComp:QueryList<SquareComponent>;

and then inside your method just use it

makeMove(){
  console.log(this.squareComp.toArray()) //here are all your square components
}

Upvotes: 1

Related Questions