![](/img/trans.png)
[英]Calling keyboard Event in subscribe method of ViewChildren & set focus on type in Angular2 typescript
[英]Angular 2 use method of dynamic ViewChildren component
我有许多通过* ngFor创建的子组件实例。 我想使用某种方法选择一个(我从父母中选择一个)。 我该怎么做?
因为ViewChild不起作用(我有很多组件)。 另外ViewChildren也无法正常工作。 因为我是在延迟后动态创建的,并且ViewChildren的长度为0。
感谢您提供任何信息。
仅举例来说。 _schedCompntnsArr的长度为0(但在页面上我有4个componnet)。
directives: [
SchedulerComponent
],
@ViewChildren(SchedulerComponent) schedCompntsList: QueryList<SchedulerComponent>;
ngAfterViewInit() {
this._schedCompntnsArr = this.schedCompntsList.toArray();
}
使用RC 4测试了以下代码,并且按预期方式工作:
@Component({
selector: 'my-app',
template: `
<FooComponent *ngFor="let item of items let i=index" [selected]="i == selected">
{{item.city}} - {{item.temp}}
</FooComponent>
`,
directives: [FooComponent]
})
class AppComponent {
public selected: number = 1;
public items: any = [{
disabled: true,
city: "Paris",
temp: 17
}, {
disabled: false,
city: "New York",
temp: 29
}, {
disabled: false,
city: "Helsinki",
temp: 23
}]
foo: any;
@ViewChildren(FooComponent) children:QueryList<FooComponent>;
ngAfterViewInit() {
this.foo = this.children.toArray();
console.log(this.foo.length); //logs 3
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.