在https://www.angular.cn 中 父組件傳值到子組件用單向綁定,此時(shí)例子中只是用了@input,并沒有用@output,但是我修改子組件里面的input里的值時(shí),卻同時(shí)將父組件中的指定的li元素的值也修改了,此時(shí)應(yīng)該怎樣避免父組件值被修改
父組件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è)不是output和input的問題, 問題的根源是引用值
和原始值
的問題
@Input()
private hero2: Hhh2;
hero2是個(gè)對(duì)象,對(duì)象在js中是引用值
, 所以你在子組件中修改了hero2,父組件中的值當(dāng)然就改變了,因?yàn)槎贾赶蛲粋€(gè)內(nèi)存地址。
你可以嘗試傳入的值是一個(gè)string, number, boolean 這種原始值, 你再試試, 你就能理解了。
在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ì)隨他的改變而改變。
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問,美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。