【发布时间】:2016-12-02 14:19:23
【问题描述】:
我正在 Angular2 中寻找 AngularJS ISolated(= operation) 范围的类似功能。
我想更改子组件中的父组件值,这样我就不需要使用任何 EventEmitters。
以下是我的代码 sn-p。
<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 中,我无法找到更改了哪个元素值。
【问题讨论】:
-
您要更改值吗?或者只是想知道您正在处理哪个元素?
标签: angular angular2-directives