鍍金池/ 問答/HTML5  HTML/ angular4中子組件改變值不會(huì)影響父組件

angular4中子組件改變值不會(huì)影響父組件

問題描述

https://www.angular.cn 中 父組件傳值到子組件用單向綁定,此時(shí)例子中只是用了@input,并沒有用@output,但是我修改子組件里面的input里的值時(shí),卻同時(shí)將父組件中的指定的li元素的值也修改了,此時(shí)應(yīng)該怎樣避免父組件值被修改

相關(guān)代碼

父組件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 = { //在oninit里面只能先對(duì)象繼承然后再對(duì)象賦值,對(duì)象聲明要在oninit外用構(gòu)造函數(shù)進(jìn)行

id: 1,
name: 'ww'

}
ok: string;
heros = HEROES;//列表的mockdata
selectedHero: Hhh;

constructor() {

}

ngOnInit() {

this.ok = 'ss';

}
onselect(hero2: Hhh): void {

this.selectedHero = hero2;

}
}

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

///////////////////////////////////////////////////////////////////////////
父組件的html
<div class = "showValue">
組件id號(hào):{{para.id}}
組件名字:{{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>

</ul>

</div>

<app-show-value-detail [hero2]="selectedHero"></app-show-value-detail>
///////////////////////////////////////////////////////////////////////////

子組件html:
<div *ngIf="hero2">
<div>被選中的名字是:{{hero2.name|uppercase}}</div>
<!--<input [(ngModel)]="hero2.name" placeholder="name">-->
</div>
///////////////////////////////////////////////////////////////////////////

子組件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;
}

回答
編輯回答
情皺

你這個(gè)不是outputinput的問題, 問題的根源是引用值原始值的問題

@Input()
private hero2: Hhh2;

hero2是個(gè)對(duì)象,對(duì)象在js中是引用值, 所以你在子組件中修改了hero2,父組件中的值當(dāng)然就改變了,因?yàn)槎贾赶蛲粋€(gè)內(nèi)存地址。

你可以嘗試傳入的值是一個(gè)string, number, boolean 這種原始值, 你再試試, 你就能理解了。

以下是原始值和引用值的基礎(chǔ)知識(shí)

在ECMAscript中,變量可以存放兩種類型的值,即原始值和引用值
原始值指的是代表原始數(shù)據(jù)類型的值,也叫基本數(shù)據(jù)類型,包括:Number、Stirng、Boolean、Null、Underfined
引用值指的是復(fù)合數(shù)據(jù)類型的值,包括:Object、Function、Array、Date、RegExp
根據(jù)數(shù)據(jù)類型不同,有的變量?jī)?chǔ)存在棧中,有的儲(chǔ)存在堆中。具體區(qū)別如下:
原始變量及他們的值儲(chǔ)存在棧中,當(dāng)把一個(gè)原始變量傳遞給另一個(gè)原始變量時(shí),是把一個(gè)棧房間的東西復(fù)制到另一個(gè)棧房間,且這兩個(gè)原始變量互不影響。
引用值是把 引用變量的名稱儲(chǔ)存在棧中,但是把其實(shí)際對(duì)象儲(chǔ)存在堆中,且存在一個(gè)指針由變量名指向儲(chǔ)存在堆中的實(shí)際對(duì)象,當(dāng)把引用對(duì)象傳遞給另一個(gè)變量時(shí),復(fù)制的其實(shí)是指向?qū)嶋H對(duì)象的指針,此時(shí) 兩者指向的 是同一個(gè)數(shù)據(jù),若通過方法改變其中一個(gè)變量的值,則訪問另一個(gè)變量時(shí),其值也會(huì)隨之加以改變;但若不是通過方法 而是通過 重新賦值 此時(shí) 相當(dāng)于 重新開了一個(gè)房間 該值的原指針改變 ,則另外一個(gè) 值 不會(huì)隨他的改變而改變。
2017年9月16日 01:55