According to my knowledge input is one way communication. Any changes made to the @Input() variable inside the child will not be reflected in the parent component.
I have a empty array in app component. App component have two child components. One is form component(to add new entries) and second is list component(to show all entries). That empty array is passed to form comp with the help of Input. But change in Input() array inside form component also reflects in root component. And list component gets updated. Here is the code:
app.component.ts
@Component({
selector: 'app-root',
template: `<div>
<app-form [formElements]="serverElements"> </app-form>
<app-list *ngFor = "let server of serverElements" [listServer]="server">
<app-list>
</div>`
})
export class AppComponent {
serverElements = [];
}
app-form.component.html
<div class="row">
<div class="col-xs-12">
<p>Add new Servers or blueprints!</p>
<label>Server Name</label>
<input type="text" class="form-control" [(ngModel)]="newServerName">
<label>Server Content</label>
<input type="text" class="form-control" [(ngModel)]="newServerContent">
<br>
<button
class="btn btn-primary"
(click)="onAdd()">Add Server</button>
</div>
</div>
app-form.component.ts
export class FormComponent {
@Input() Elements=[];
newServerName = '';
newServerContent = '';
constructor() { }
onAdd() {
this.Elements.push({
name: this.newServerName,
content: this.newServerContent
});
}
}
app-list.component.ts
@Component({
selector: 'app-server-element',
template: ` <div class="panel-heading">{{ server.name }}</div>
<div class="panel-body">
<p>{{server.content}}</p>
</div>`,
})
export class ListComponent implements {
@Input() server;
constructor() { }
}