Prateek Pareek
Prateek Pareek

Reputation: 170

Reactive Form Data Binding with Angular Service

I have 5 forms so I am implementing data service to connect them. Service working fine and I am getting data in service but when I will go from one page to the second page and click on next them I will only get second form data but I want first form data also So that in final stage I can send all collected data to API. this is the my code

`import { Injectable } from '@angular/core';`
`import { AddBusinessMutation} from 'src/app/graphql'`
`import{FormGroup, FormControl, Validators,FormArray} from '@angular/forms'`

`@Injectable({
    providedIn: 'root'
})`
`export class AddbusinessService {`
    `business: AddBusinessMutation;`
    `businesses: [];`
    `setGeneralFormData(data) {`
        `// this.businesses = data.socialLinks;
        // this.business = data.frmData;`
        `console.log(data)
    }`
`constructor(){`
    `}`
`}`

Upvotes: 0

Views: 945

Answers (2)

m.jafari
m.jafari

Reputation: 11

you can use state managment to store form data

https://netbasal.com/connect-angular-forms-to-ngrx-store-c495d17e129

Upvotes: 1

Abdelrhman ElSayed
Abdelrhman ElSayed

Reputation: 439

I think you should push every form data to businesses array and append new properties to business object

business: AddBusinessMutation;
businesses: any[] = [];
setGeneralFormData(data) {
  this.businesses.push(data.socialLinks)
  this.business = <AddBusinessMutation> Object.assign({}, this.business, data.frmData)
  console.log(data);
}

instead of

setGeneralFormData(data) {
  this.businesses = data.socialLinks;
  this.business = data.frmData;
  console.log(data)
}

Upvotes: 1

Related Questions