I have a convention object that i have to pass as an input to a child component, the convention id is displayed when i call it in the child's template, but i get undefined when i try to use my the convention in a console.log. I tried placing my console.log in the constructor of the child as well as in ngOnInit, ngAfterViewInit and ngOnChanges. For ngOnChanges when i pass only the id of the convention as an input to the child component it works just fine, but i need to pass the convention not its id.
index.component.ts
@Component({
templateUrl: './index.template.html',
selector: 'be-convention-update',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ConventionUpdateComponent {
private convention$: Convention;
private conventionId: number;
constructor(private route: ActivatedRoute,
private conventionService: ConventionService) {
this.route.params.subscribe(
(params: any) => {
this.conventionId = params['id'];
this.conventionService.getOne(this.conventionId).subscribe(response => this.convention$ = response);
}
);
}
}
index.template.html
<be-form-convention [convention]="convention$"></be-form-convention>
form.component.ts
@Component({
selector: 'be-form-convention',
templateUrl: './form.template.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ConventionFormComponent implements OnChanges, OnInit {
@Input() convention: Convention;
constructor() {
}
public ngAfterViewInit() {
console.log(this.convention); //undefined
}
public ngOnInit(): void {
console.log(this.convention); //undefined
}
public ngOnChanges(changes: SimpleChanges): void {
const object: SimpleChange = changes.convention;
console.log('prev value: ', object.previousValue); //undefined
console.log('got name: ', object.currentValue); //undefined
}
}
$
suffix screams observable and "in need of async pipe" but it's unclear to say the least what you are doing and why you are doing it. – Aluan Haddad