0

I need update 'lat' and 'lon' values inside FormArray:

initRows() {
        return this._fb.group({
            address: "",
            lat: 0,
            lon: 0,
        });
   }




 initForm() {
        this.searchForm = this._fb.group({
        addresses: this._fb.array([this.initRows()]),
    });

I need update them after callback:

 this.someService.getData().subscribe(
                  (response) => {this.data = response.json()
                                 this.lon = this.data.x;
                                 this.lat = this.data.y;
                                // UPDATE MY 'lat' and 'lon'
                  },
                  (error) => console.log('ERROR: ' + error)
          ); 

form.value object:

{

  "addresses": [
    {
      "address": {
        "text": "Weberweg, 58566, Kierspe, Nordrhein-Westfalen, DEU"
     },
      "lat": 0,
      "lon": 0
    },
     {
      "address": {
        "text": "Weberweg, 58566, Kierspe, Nordrhein-Westfalen, DEU"
     },
      "lat": 0,
      "lon": 0
    }
  ]
} 

P.S. 'lat' and 'lon' not an inputs, just data

1 Answer 1

2

I'm not sure is it "right" way, but you can do this:

    /* getter for addresses.
    get addresses() {
       this.seachForm.controls.addresses as FormArray;
    }

    /* function for loading data */
    public loadData() {
        this.someService.getData().subscribe(
           (response) => {
                          this.data = response.json()
                          this.lon = this.data.x; // it is global lon like I understood
                          this.lat = this.data.y; // it is global lat like I understood
                          this.addresses.controls.forEach((address) => {
                                 address.get('lat').setValue(this.lat);
                                 address.get('lon').setValue(this.lon);
                          }),
           (error) => console.log('ERROR: ' + error)
      );
}

Hope I got you right and this answer is helpful for you.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem is that I am trying update these values after server responce and such kind of FormGroup can be several(dynamic add/remove fields). The main point to get current FormGroup control
Can I get not hole array, just current formGroup?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.