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.

7
  • 2
    can you create a small example on stackblitz.com? Commented Aug 20, 2019 at 7:32
  • 5
    can you show the parent code and the place where you say you pass the data to the child? Commented Aug 20, 2019 at 7:33
  • 1
    How is units assigned content? Commented Aug 20, 2019 at 8:15
  • units: string[] = ['mm', 'kg', 'stk']; Commented Aug 20, 2019 at 8:19
  • 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: stackblitz.com/edit/angular-jp8che?file=app/… Commented Aug 20, 2019 at 8:34

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

well, OP says the data is assigned units: string[] = ['mm', 'kg', 'stk']; so async shouldn't be the issue.
Also there is already answered same type of question: stackoverflow.com/a/39219751/5272951 @AJT_82
what does that question have to do with this question?
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[] = []

1 Comment

I did, but I forgot to write it in my question. I edited my question.
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!

Comments

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.