如何从angular2中的子组件更新父组件
how to update parent component from child component in angular2
我正在寻找 Angular2 中的 AngularJS ISolated(= operation) 作用域类似功能。
我想更改子组件中的父组件值,这样我就不需要使用任何 EventEmitter。
以下是我的代码片段。
<component-1>
<div *ngFor="let row of listArray" >
<component-2 [inputData]="row.inputData" (outputEvent)= "onComponentChange($event)"> </component-2>
</div>
<component-2 [inputData]="inputData2" (outputEvent)= "onComponentChange($event)"> </component-2>
<component-2 [inputData]="inputData3" (outputEvent)= "onComponentChange($event)"> </component-2>
<component-2 [inputData]="inputData4" (outputEvent)= "onComponentChange($event)"> </component-2>
@Component
component-1{
onComponentChange(newValue){
//where to keep the new value
//this.inputData2/inputData3/inputData4/listArray[i].inputData ???????????
}
}
@Component
component-2{
@Input() inputData:string;
@Output() outputEvent:EventEmitter<string>;
changeComponentValue(newValue){
this.outputEvent(newValue);
}
}
我将更改组件 2 中的 [inputData] 值,该值应反映在组件 1 中。
在现有的@Output eventEmitter 中,我无法找到更改了哪个元素值。
在这里,我将向您展示如何识别您正在处理的元素的 索引 以及如何为您正在处理的元素分配新值。
要为 row.inputData 分配一个新值我正在处理与 @Input xxx 的双向数据绑定;和 @Output xxxChange 语法。
要识别您正在处理的元素的 index 我只是使用新的 @output api.
冷静观察这段代码。
@Component({
selector: 'my-app',
directives:[ChildComponent],
template:`<h1>My First Angular 2 App</h1>
<div *ngFor="let row of listArray" >
{{row.inputData}}
<component-2 [(inputData)]="row.inputData" (outputEvent)= "onComponentChange($event)"> </component-2>
</div>
`
})
export class AppComponent {
title="Angular1";
listArray=[{inputData:"micronyks"},{inputData:"micronyks1"},{inputData:"micronyks3"}];
onComponentChange(value){
console.log(value);
this.listArray.forEach((item,index)=>{
if(item.inputData==value){
console.log(index);
}
})
}
}
组件 2
import { Component, Input,Output,EventEmitter } from '@angular/core';
@Component({
selector: 'component-2',
template:`<button (click)="changeComponentValue(inputData)">Click-{{inputData}}</button>`
})
export class ChildComponent {
@Input() inputData;
@Output() outputEvent:EventEmitter<string>=new EventEmitter();
@Output() inputDataChange=new EventEmitter();
changeComponentValue(value){
this.outputEvent.emit(value); //<---this will identify element index you are dealing with
this.inputDataChange.emit("Angular2"); //<----new value will be assinged to an element that you are dealing with
}
}
我正在寻找 Angular2 中的 AngularJS ISolated(= operation) 作用域类似功能。
我想更改子组件中的父组件值,这样我就不需要使用任何 EventEmitter。
以下是我的代码片段。
<component-1>
<div *ngFor="let row of listArray" >
<component-2 [inputData]="row.inputData" (outputEvent)= "onComponentChange($event)"> </component-2>
</div>
<component-2 [inputData]="inputData2" (outputEvent)= "onComponentChange($event)"> </component-2>
<component-2 [inputData]="inputData3" (outputEvent)= "onComponentChange($event)"> </component-2>
<component-2 [inputData]="inputData4" (outputEvent)= "onComponentChange($event)"> </component-2>
@Component
component-1{
onComponentChange(newValue){
//where to keep the new value
//this.inputData2/inputData3/inputData4/listArray[i].inputData ???????????
}
}
@Component
component-2{
@Input() inputData:string;
@Output() outputEvent:EventEmitter<string>;
changeComponentValue(newValue){
this.outputEvent(newValue);
}
}
我将更改组件 2 中的 [inputData] 值,该值应反映在组件 1 中。
在现有的@Output eventEmitter 中,我无法找到更改了哪个元素值。
在这里,我将向您展示如何识别您正在处理的元素的 索引 以及如何为您正在处理的元素分配新值。
要为 row.inputData 分配一个新值我正在处理与 @Input xxx 的双向数据绑定;和 @Output xxxChange 语法。
要识别您正在处理的元素的 index 我只是使用新的 @output api.
冷静观察这段代码。
@Component({
selector: 'my-app',
directives:[ChildComponent],
template:`<h1>My First Angular 2 App</h1>
<div *ngFor="let row of listArray" >
{{row.inputData}}
<component-2 [(inputData)]="row.inputData" (outputEvent)= "onComponentChange($event)"> </component-2>
</div>
`
})
export class AppComponent {
title="Angular1";
listArray=[{inputData:"micronyks"},{inputData:"micronyks1"},{inputData:"micronyks3"}];
onComponentChange(value){
console.log(value);
this.listArray.forEach((item,index)=>{
if(item.inputData==value){
console.log(index);
}
})
}
}
组件 2
import { Component, Input,Output,EventEmitter } from '@angular/core';
@Component({
selector: 'component-2',
template:`<button (click)="changeComponentValue(inputData)">Click-{{inputData}}</button>`
})
export class ChildComponent {
@Input() inputData;
@Output() outputEvent:EventEmitter<string>=new EventEmitter();
@Output() inputDataChange=new EventEmitter();
changeComponentValue(value){
this.outputEvent.emit(value); //<---this will identify element index you are dealing with
this.inputDataChange.emit("Angular2"); //<----new value will be assinged to an element that you are dealing with
}
}