0

I have a child component implemented in the parent component where I pass data to the child component. But when I try to use the array, I always get an undefined.

How can I fix this?

export class SelectionFieldComponent implements OnInit {

  @Input() data: any[] = [];

  constructor() { }

  ngOnInit() {
    console.log(this.data);
  }
}

parent.component.html

<app-selection-field [data]="units"></app-selection-field>

EDIT: I see the data on the site, but also get the error.

lrefopam
  • 511
  • 1
  • 5
  • 22
  • 2
    can you create a small example on stackblitz.com? – alt255 Aug 20 '19 at 07:32
  • 5
    can you show the parent code and the place where you say you pass the data to the child? – Llorenç Pujol Ferriol Aug 20 '19 at 07:33
  • 1
    How is `units` assigned content? – AT82 Aug 20 '19 at 08:15
  • units: string[] = ['mm', 'kg', 'stk']; – lrefopam Aug 20 '19 at 08:19
  • Please provide a stackblitz which reproduces your issue. Now seeing how you assign data, your code should work fine. And you say `I also get the error`, what error would that be? since you also say that you can see the data. – AT82 Aug 20 '19 at 08:22
  • https://stackblitz.com/edit/angular-grx2rf – lrefopam Aug 20 '19 at 08:27
  • 1
    When you provide a stackblitz, please make sure that it is a **working** stackblitz. Here is a **working** stackblitz, and we can see that the code you provided works just like it should: https://stackblitz.com/edit/angular-jp8che?file=app/form-field-overview-example.ts – AT82 Aug 20 '19 at 08:34

3 Answers3

2

It might depend on how do you create your data array.

  • If it's from observable:

1st solution to implement OnChanges interface. Because observable gets data asynchronous and just then parses to the component. So you might not be able to print data in ngOnInit() method, because data might come just after component initialization.

ngOnChanges(changes: SimpleChanges): void {
    if (changes['data']) {
        this.data = changes['data'].currentValue;
    }
}

2nd solution
Use setTimeout() method, but it's very bad practice.


  • If it's a static (hard coded) array

It should work as it is. Or try to use same methods which is above.


Also please read about component lifecycle

Dumbo
  • 1,630
  • 18
  • 33
  • well, OP says the data is assigned `units: string[] = ['mm', 'kg', 'stk'];` so async shouldn't be the issue. – AT82 Aug 20 '19 at 08:44
  • Also there is already answered same type of question: https://stackoverflow.com/a/39219751/5272951 @AJT_82 – Dumbo Aug 20 '19 at 08:47
  • what does that question have to do with this question? – AT82 Aug 20 '19 at 08:51
1

You haven't included any alises. So your parent property binding of parent should be data

<app-parent [data]="your-array"></app-parent>

In your child component, as you defined

@Input() data:any[] = []
Ruwandi
  • 41
  • 3
0

I have figured out where the error comes from. In my parent component I have multiple selection-fields and one of them don't received the data correctly so it is undefined. I thought that was the selection field we are talking about. After a quick if/else it is working now. Thank you all!

lrefopam
  • 511
  • 1
  • 5
  • 22