I have created a user photo component
which takes an @Input()
value, this value is the userId. If the value is passed in then I retrieve information from Firebase
linking to this userId, if it doesn't then I do something else.
My user-photo component
import { Component, OnInit, OnDestroy, Input } from '@angular/core';
@Component({
selector: 'user-photos',
templateUrl: './user-photos.component.html',
styleUrls: ['./user-photos.component.css']
})
export class UserPhotoComponent implements OnInit {
@Input() userId: string;
constructor() {
console.log('userId is:',this.userId);
}
ngOnInit() {
}
ngOnDestroy() {
}
}
As you can see I have declared the userId as @Input()
, now on my edit-profile component
I have the following:
<user-photos [userId]="TestingInput"></user-photos>
Now the User Photo component gets rendered as I see the h1
tag appearing, however the userId is always undefined ?
No errors are appearing in the developer console, so I'm not entirely sure what I've done wrong.
It will be initialized in ngOnInit
, not the constructor. (Please also review the Angular Life Cycle Hooks documentation.)
ngOnInit() {
console.log('userId is:',this.userId);
}
Also if you want to pass a literal like a string and use brackets []
you have to pass it as a string by enclosing the value with single ticks.
<user-photos [userId]="'TestingInput'"></user-photos>
The reason for that is the containing expression is evaluated in the context of the containing component so without it it will try to retrieve and pass a property/field named TestingInput
which will be undefined (unless you also happen have a field by that name).
In my case i had to use *ngIf = "isLoaded" on the parent's template first.
On Parent Component
<div *ngIf = "isLoaded">
<app-child-component [dataToChild]="dataFromParent"> </app-child-component>
</div>
On Child Component
@Input() dataToChild: any;
constructor() { }
ngOnInit(): void {
console.log(this.dataToChild);
}
If your input comes delayed (e.g. a slow webservice), the value will at first be undefined until the response arrives from the webservice.
In order to debug this, you could use ngOnChanges() hook, which will fire every time, an input value changes:
ngOnChanges() {
console.log('data', this.data);
}
which will output something like:
data undefined
<- OnInit, value did not arrive yet
data here it is!
<- value arrives delayed
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