在方法中动态获取组件并以角度设置其属性

问题描述 投票:0回答:1

我正在开发一款离子游戏,如何通过父组件(应用板)中的方法获取子组件(应用正方形)的属性。应用板上有30多个应用方形组件,因此我希望它能够动态访问它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
    }

我知道我无法使用ViewChild,因为它在方法内部不起作用。

angular ionic-framework
1个回答
0
投票

使用这样的板子组件内部的ViewChildren装饰器对其进行引用

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

然后在您的方法内部使用它

makeMove(){
  console.log(this.squareComp.toArray()) //here are all your square components
}
© www.soinside.com 2019 - 2024. All rights reserved.