0

I have many instances of child component that creates via *ngFor. I want to use some method of selected one (I select one from parent). How I can do this?

Because ViewChild is not works (I have many components). Also ViewChildren also not working. Because I created it dynamically after some delay and ViewChildren have 0 length.

Thanks for any information.

just for example. _schedCompntnsArr have 0 length (but on page I have 4 componnets).

 directives: [
    SchedulerComponent
  ],

@ViewChildren(SchedulerComponent) schedCompntsList: QueryList<SchedulerComponent>;

ngAfterViewInit() {
  this._schedCompntnsArr = this.schedCompntsList.toArray();
}

1 Answer 1

1

Tested the following code with R.C 4 and it worked as expected:

@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
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Did you try to fill items by data after some delay ? 5 sec for example.
async operations are different story. See this sample: plnkr.co/edit/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.