0

I've the following JSON struct

{
"first_name": "Peter",
"surname": "Parker",
"adresses": {
    "adress": [{
        "info1": "intern",
        "info2": "bla1"
    }, {
        "info1": "extern",
        "info2": "bla2"
    }, {
        "info1": "group",
        "info2": "bla3"
    }, {
        "info1": "outdoor",
        "info2": "bla4"
    }, {
        "info1": "indoor",
        "info2": "bla5"
    }]
}}

Q: How can i make a *ngFor about all addesses?

Thanks in advance and regards Filout

4 Answers 4

2

you can do it like this

in ts file

this.adressList= data.adresses.adress

in html file

<div *ngFor="let adress of adressList">
   {{adress.info1}}
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

do like this.

in your component.ts file

data = {
    "first_name": "Peter",
    "surname": "Parker",
    "adresses": {
        "adress": [{
            "info1": "intern",
            "info2": "bla1"
        }, {
            "info1": "extern",
            "info2": "bla2"
        }, {
            "info1": "group",
            "info2": "bla3"
        }, {
            "info1": "outdoor",
            "info2": "bla4"
        }, {
            "info1": "indoor",
            "info2": "bla5"
        }]
    }
  }
  address = this.data.adresses.adress

in html

<div *ngFor="let value of address">
  {{value.info1}} - {{value.info2}}
</div>

let me know if you have any doubt.

Comments

0

Thanks for your quick reply. But what does ERROR TypeError: this.form._updateTreeValidity is not a function mean in the following code?

<div *ngFor="let address of addressList">
    <div [formGroup]="address">
      <input formControlName="info1">
    </div>
</div>

1 Comment

please check this link for more info on this error stackoverflow.com/questions/40898056/…
0

I read that but i don't understand the solution. I tried the following in ".ts":

constructor(...) {
    this.formGroup = this.fb.group(...)
    this.formGroup.addControl('addresses', this.fb.group({
        address: new FormArray([])
    }))

That works for me. Than i've another function like this:

public get objControl(): FormArray {
        return this.formGroup.controls.addresses.controls.address as FormArray
    }

If i set a breakpoint in constructor i can see this.formGroup.controls.addresses.controls.address with values but my IDE (Webstorm) say "Property 'controls' does not exist on type 'AbstractControl'"

Why this?

Comments

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.