Here I will show you how to identify the index of the item you are dealing with, and how to assign a new value to the item you are dealing with.
To assign a new value to row.inputData strong>, I am dealing with TWO-WAY data binding with @Input xxx ; and @Output xxxChange .
To determine the index of the item you are dealing with, I just use the new @output api.
follow this code calmly.
@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); } }) } }
component 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);
Working demo: https://plnkr.co/edit/SqrfhtZZlSIsQE0Ko0oC?p=preview
source share