I'm using Angular 12.0.4
.
My template has an Input like this:
<input type="text" [(ngModel)]="end.itemOne">
<button (click)="resetField()">reset</button>
<p>why does <strong style="color:red">this.start</strong> change with input value? </p>
<strong>{{ this.start.itemOne }}</strong>
<br>
<br>
<p>I need only <strong style="color:blue">this.end</strong> to change </p>
<strong>{{ this.end.itemOne }}</strong>
Inside the component:
start: any = {
itemOne: 'item1',
itemTwo: 'item2'
};
end!: any;
ngOnInit(): void {
this.end = this.start;
}
resetField(): void {
this.end = this.start;
}
Here is a stackblitz
showing the issue:
https://stackblitz.com/edit/angular-njppqj?file=src%2Fapp%2Fapp.component.ts
My problem is: If I change the input value and then click on the reset button
I want this.end.itemOne
value to become 'item1' again. Instead, when I change the input value, both this.start
and this.end
become equal in value.
Thanks in advance.