Я создал компонент, который принимает значение @Input()
, но всегда получаю undefind
. Вот мой код:
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)
}
}
Если я используйте ngOnChanges()
в детях, чем сначала выведите меня:
data undefined
, а затем:
data Array(6)
Как я могу получить это значение в onInit
?
Буду очень признателен за любую помощь!