I've created component which takes an @Input() value but I always get undefind.
Here's my code:
parent.html
<added-item
[minItemValueLength]="minItemValueLength"
[maxItemValueLength]="maxItemValueLength"
[listOfItems]="listOfItems"
></added-item>
children.ts
export class AddedItemComponent implements OnInit, OnChanges{
@Input() minItemValueLength: number;
@Input() maxItemValueLength: number;
@Input() listOfItems: string[];
ngOnInit() {
console.log(this.minItemValueLength) // return 3
console.log(this.listOfItems) // return undefined
}
}
parent.ts
export class LocationsTabComponent implements OnInit {
listOfItems: string[];
minItemValueLength = 3;
maxItemValueLength = 15;
public ngOnInit(): void {
this.locationService.getLocations().subscribe((locations) => {
this.getLocationsList();
console.log(this.listOfItems) // return Array(6)
});
}
private getLocationsList() {
this.listOfItems = this.locations.map((location: Location) => location.name);
console.log(this.listOfItems) // return Array(6)
}
}
If I use ngOnChanges() in children than it at first output me:
data undefined and then:
data Array(6)
How can I get this value in onInit?
Would be really grateful for any help!