【发布时间】:2019-04-16 03:55:53
【问题描述】:
我有一组卡片对象
...
cards: Card[];
...
ngOnInit() {
this.cardService.getCards.subscribe(r => { this.cards = r; });
}
我这样在模板中添加子卡片组件
<div id="cards-container">
<app-card *ngFor="let card of cards" [name]="card.name"></app-card>
</div>
卡片组件有一个名字和一些样式,依赖于一个通过点击组件来切换的活动属性
@Component({
selector: 'app-card',
templateUrl: './card.component.html',
'styleUrls: ['./card.component.scss']
})
export class CardComponent {
private _name = '';
private _active = false;
// getters/setters
...
onClick(e) {
this.active = !this.active;
}
}
card.component.html
<div [ngClass]="{'card': true, 'active': _active}"
(click)="onClick($event)">
{{name}}
</div>
所有这些都很好。
问题:
在父组件中,我需要遍历所有使用*ngFor="let card of cards" 添加的卡片组件,并将它们全部设置为活动或不活动,但我不知道该怎么做。
我尝试过的:
我尝试使用@ViewChildren(),但QueryList 的toArray() 方法总是给我一个空数组。从ViewChildren documentation 中的有限示例中,我不是 100% 清楚是否需要在父组件中添加额外的指令,或者示例中的指令是否只是用于演示。所以我尝试的是
@ViewChildren(CardComponent) cardList: QueryList<CardComponent>;
我也尝试使用ViewContainerRef 使用类似于this answer 的东西,但我无法使其工作,而且似乎这并没有让我朝着正确的方向前进。我还查看了the documentation for ComponentRef,但我不知道这如何或是否可以帮助我解决我的问题。
感谢任何可以为我指明正确方向的建议。
更新
我在卡片组件中的active setter是这样的
@Input()
set active(active: boolean) {
this._active = active;
}
我需要能够随时更改所有卡片的此项,换句话说,“全选/取消全选”选项。
已解决!
使用suggestion from @Tim Klein,我订阅了QueryList 的changes,并且能够将我的组件放入一个数组中,当QueryList 更改时我会更新该数组。现在我只是迭代组件数组并调用我的active setter。
cards: CardComponent[];
@ViewChildren(CardComponent) cardList: QueryList<CardComponent>;
...
ngAfterViewInit(): void {
this.cards = this.cardList.toArray(); // empty array but that's okay
this.cardList.changes.subscribe((r) => {
this.cards = this.cardList.toArray(); // we get all the cards as soon as they're available
});
}
【问题讨论】:
-
哈哈我喜欢你的用户名
标签: angular dynamically-generated