-1

created app with two components(A&B) with popup dialog on edit:

  • Comp A Fetch the data from service and load to the data table

  • Comb B initiates the data when pop event fired from A.

typically multiple records are load with array response into the table.

when pop is fired form data loaded correctly for record 1 for record 2 the same value of 1 is getting loaded instead of 2.

used MAT_DIALOG_DATA to import the table data to B Component.

a unique record to populate in form B

A_component.ts:

open() const dialogconfig = new MatDialogConfig(); 
dialogconfig.diableClose =true; 

this.dialog.open(BComoonent,
{ 
  data: this.datasource
});

BComponent.ts

constructor(@inject((MAT_DIALOG_DATA public) data:any;)

and HTML Code:

<input matInput placeholder="First Name " id="" name=" [ngModel]="data[0].empName">
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
vijay munnangi
  • 43
  • 1
  • 4
  • 13
  • Nice. So show us your code please. –  Jan 31 '19 at 22:06
  • A component code ts open() const dialogconfig = new MatDialogConfig(); dialogconfig.diableClose =true; this.dialog.ope(BComoonent name ,{ data: this.datasource}--which contains reocord. – vijay munnangi Feb 01 '19 at 00:10
  • component B code .ts constructor (@inject((MAT_DIALOG_DATA public) data:any;) html code typically when i observe and change the [ngModel]="datat[1].emName"> i see differnt values other than array.the array 1 data for all rows – vijay munnangi Feb 01 '19 at 00:25

1 Answers1

0

The same value is displayed in the dialog because you are explicitly binding the input element value via ngModel to the first row of your data in this line:

<input matInput placeholder="First Name " id="" name=" [ngModel]="data[0].empName">

data[0].empName will always display the empName of the first record because you are directly accessing it via data[0].

What you should do instead is, passing only the row you want to edit and not the complete datasource.

In your component A you need to replace your datasource with the row the edit action was fired on:

this.dialog.open(BComponent,
{ 
  data: this.datasource // --> change this to the row you want to edit
});

And in your dialog you can then access the data without the index, as it is not an array e.g. not the complete datasource:

<input matInput placeholder="First Name " id="" name=" [ngModel]="data.empName">
Fabian Küng
  • 5,925
  • 28
  • 40
  • Appreciate your response and I could say this works well :) – vijay munnangi Feb 01 '19 at 20:24
  • One more trouble to posting the data to api , my api requesting three parameters as 1 :1 and 2:2 and json body. the dialog form is reactive form. Can we extract the Params and send this to service? – vijay munnangi Feb 05 '19 at 01:38