I'm learning Angular 4 following and adapting the Angular heroes tutorial and I have a problem when rendering a child component.
The child component (QvSummaryComponent) should display two properties (code and versionNumber) of the model (QvSetup) loaded by the parent child (QvSetupComponent).
app.module.ts:
@NgModule({
imports: [BrowserModule],
declarations: [
QvSetupComponent,
QvSummaryComponent,
QvOtherChildComponent],
bootstrap: [
QvSetupComponent,
QvSummaryComponent,
QvOtherChildComponent]
})
setup.model.ts:
export class QvSetup {
code: string;
versionNumber: number;
}
setup.component.ts (parent component):
export class QvSetupComponent implements OnInit {
private qvSetup: QvSetup;
constructor(private readonly qvSetupService: QvSetupService) { }
ngOnInit() {
this.loadQvSetup();
}
loadQvSetup(): void {
// Hardcoded for the moment. It will come from the service later.
this.qvSetup = new QvSetup();
this.qvSetup.code = "MYCODE";
this.qvSetup.versionNumber = 1;
// Debug message (to remove)
console.info("QvSetupComponent: loaded " + this.qvSetup.code + "-" + this.qvSetup.versionNumber);
}
}
setup.component.html:
<h1>Setup <small>{{qvSetup.code}}{{qvSetup.versionNumber}}</small></h1>
<qvSummary
[code]="qvSetup.code"
[versionNumber]="qvSetup.versionNumber">
Loading summary...
</qvSummary>
<qvOtherChildComponent>
Loading other child component...
</qvOtherChildComponent>
qvSummary.component.ts:
export class QvSummaryComponent implements OnInit {
@Input() code: string;
@Input() versionNumber: number;
private qvSummary: QvSummary;
constructor() {
this.qvSummary = new QvSummary();
}
ngOnInit() {
if (this.code != null) {
console.info("QvSummaryComponent: Code-VersionNumber = " + this.code + "-" + this.versionNumber);
} else {
console.info("QvSummaryComponent: Code-VersionNumber = null");
}
}
}
qvSummary.component.html:
{{qvSetup.code}}-{{qvSetup.versionNumber}}
When I execute my script, I can see in the console:
QvSetupComponent: qvSetup loaded MYCODE-1
QvSummaryComponent: code-versionNumber = MYCODE-1
QvSummaryComponent: code-versionNumber = null
It seems that it is rendering twice the child component. What I'm doing wrong?