I am trying to pass a map, which come from an api, from parent to child component, in angular 7.
parent.ts:
export class AppComponent {
title = 'taurs-frontend';
categories: any;
isLoggedIn = false;
ngOnInit(){
this.roomDataService.getData(`${environment.api_url}/api/Categories`)
.subscribe(categories => {
this.categories=categories
});
}
parent.html:
<app-room-card [categories]="categories"></app-room-card>
child.ts:
@Component({
selector: 'app-room-card',
templateUrl: './room-card.component.html',
styleUrls: ['./room-card.component.css']
})
export class RoomCardComponent implements OnInit {
@Input('categories') catname: any;
ngOnInit() {
console.log('aaa'+ this.catname);
}
// ..
}
When I try to log the variable catname
, it is undefined. If I try to import variable title from the parent, everything works properly. How can I pass categories to the child, filling it with the values from the API call?
You are trying to pass asynchronous data to child component. You have different solutions to do that. For exemple, you can use ngOnChanges
instead of ngOnInit
:
ngOnChanges() {
console.log('aaa'+ this.catname);
}
Another solution is to use *ngIf
, in order to delay the initialization of posts components:
<app-room-card *ngIf="categories" [categories]="categories"></app-room-card>
Take a look at this link: https://scotch.io/tutorials/3-ways-to-pass-async-data-to-angular-2-child-components#toc-solution-2-use-ngonchanges
Try changing your component code as,
export class RoomCardComponent implements OnInit {
@Input() categories: any;
ngOnInit() {
console.log('aaa'+ this.categories);
}
}
and have *ngIf on the parent component just to make sure the data is passed to child once you get the response from your API
<app-room-card *ngIf="categories" [categories]="categories"></app-room-card>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With