Changing the value of the angular4 subcomponent does not affect the parent component.

problem description

in https://www.angular.cn, the parent component passes the value to the child component with one-way binding. In this case, @ input, is used instead of @ output,. But when I modify the value in the input in the child component, I also change the value of the specified li element in the parent component. At this time, how to prevent the parent component value from being modified

related codes

parent component ts:
import {Component, OnInit} from"@ angular/core";
import {HEROES} from".. / mock-data";

@ Component ({
selector: "app-show-value",
templateUrl:". / show-value.component.html",
styleUrls: [". / show-value.component.css"]
})
export class ShowValueComponent implements OnInit {
para: Hhh = {/ / in oninit only object inheritance is followed by object assignment. Object declaration should be performed with constructor outside oninit

.
id: 1,
name: "ww"

}
ok: string;
heros = mockdata
selectedHero: Hhh;

of the HEROES;// list

constructor () {

}

ngOnInit () {

this.ok = "ss";

}
onselect (hero2: Hhh): void {

)
this.selectedHero = hero2;

}
}

export class Hhh {
id: number;
name: string;
}

/ /
html
< div class = "showValue" >
component id number: {{para.id}}
component name: {{para.name | uppercase}}
< input type= "text "[(ngModel)] =" para.name "placeholder=" name ">
< ul class=" herosStyle ">

<li *ngFor="let herolist of heros" (click)="onselect(herolist)">
  <span>{{herolist.name}}</span>
</li>

< / div >

< / app-show-value-detail >
/ /

subcomponent html:
< div * ngIf= "hero2" >

the selected name is: {{hero2.name | uppercase}} < / div >

< / div >
/ / /

Sub-component ts:
import {Component, Input, OnInit} from"@ angular/core";
/ / import {Hhh} from".. / show-value/show-value.component";

@ Component ({
selector: "app-show-value-detail",
templateUrl:". / show-value-detail.component.html",
styleUrls: [". / show-value-detail.component.css"]
})
export class ShowValueDetailComponent implements OnInit {
@ Input ()
private hero2: Hhh2;

constructor () {}

ngOnInit () {

setInterval(() => {
  this.hero2.name = "sss";
});

}

}
export class Hhh2 {
id: number;
name: string;
}

Jun.17,2021

your problem is not the problem of output and input , but the root of the problem is the problem of reference value and original value

.
@Input()
private hero2: Hhh2;

hero2 is an object, and the object is the reference value in js, so if you modify the value in the hero2, parent component in the child component, it will change, of course, because it all points to the same memory address.

you can try passing in a primitive value like string, number, boolean, and you'll understand if you try again.

the following are the basics of original and referenced values

in ECMAscript, variables can store two types of values, namely, the original value and the reference value
the original value refers to the value that represents the original data type, also known as the basic data type, including: Number, Stirng, Boolean, Null, Underfined
reference value refers to the value of the compound data type, including: Object, Function, Array, Date, RegExp
depending on the data type, some variables are stored in the stack, some variables are stored in the heap. The specific differences are as follows:
original variables and their values are stored in the stack. When one original variable is passed to another original variable, something from one stack room is copied to another stack room, and the two original variables do not affect each other.
reference value stores the name of the reference variable in the stack, but stores its actual object in the heap, and there is a pointer from the variable name to the actual object stored in the heap. When the reference object is passed to another variable, the pointer to the actual object is actually copied. At this time, the two point to the same data. If you change the value of one variable by method, then access the other variable. Its value will also be changed accordingly; However, if the original pointer of the value is changed not through the method but by re-assigning the value, the other value will not change with his change.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b4383d-2c694.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b4383d-2c694.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?