I have a BoxComponent that has only one field, an array of numbers. The selector for this Component is app-box
box.component.html
<p>
<span *ngFor="let num of cards">Number is {{num}}</span>
</p>
box.component.ts
export class BoxComponent {
cards: number[] = [];
top_up(val: number) {
this.cards.push(val);
}
}
In its parent AppComponent, I added a single field that stores a BoxComponent object.
app.component.html
<app-box></app-box>
<button (click)="push()">Top-up</button>
app.component.ts
export class AppComponent {
title = 'test';
box: BoxComponent = new BoxComponent();
push() {
this.box.top_up(Math.floor(Math.random() * 10));
}
}
I wish to make the click in the appcomponent template to modify the contents of app-box.
However angular does not update the app-box when the button is clicked. I somehow was able to confirm that the field box of appcomponent actually gets updated, the change just doesn't show in the browser. Based on this I have concluded that the app-box tag creates a different BoxComponent rather than use the one I have created within the AppComponent class.
Am I right? How do I make angular render my box and respond to changes I make to it from the AppComponent click event?
Edit: I have simplified the problem to its simplest form.