I encountered a wizard problem in angular5. I have one component parent which send data binding in a child component, but the data binded are well display in child html but not in the child component.
Parent Component
@Component({
selector: 'app-element',
templateUrl: '../templates/app.element.component.html',
})
export class AppElementComponent implements OnInit {
titles: {} = {};
constructor() {}
ngOnInit(): void {
// Details
this.restService.postDb('url')
.subscribe(response => {
this.titles = response.title;
// this.titles = { 'menu1': 'something', 'menu2': 'somethingelse'}
});
}
}
Parent Template
<div>
<app-panel [titles]="titles"></app-panel>
</div>
Child Component
@Component({
selector: 'app-panel',
templateUrl: '../templates/app.panel.component.html',
})
export class AppPanelComponent implements OnInit {
@Input() titles;
constructor() {}
ngOnInit() {
//I put a timeout just to wait the binding
setTimeout(function() {
console.log(typeof this.titles);
console.log(this.titles);
}, 5000);
}
}
Child Template
<pre>{{ titles | json}}</pre>
<pre>{{ titles.keys | json}}</pre>
In the child template, I have the first pre with the titles data, but the second pre doesn't work. And in the child component, the both console log return undefined. I don't understand why values of same variable are different in template and component.
Thanks.