2

Hey I know this question comes up a lot but the normal fixes do not work.

ts

onChangeFormType(changeFormType) {
this.serverData.getData('questionnaire/' + changeFormType)
  .subscribe(
    (response: Response) => {
      this.formType = response.json();
      let key = Object.keys(this.formType);
      for (let i = 0; i < key.length; i++) {
        this.currentValue.push(this.formType[key[i]])
      }
      console.log('current Value', this.currentValue);
    },
    (error) => console.log('Form Error', error)
  )}

In console.log('current Value', this.currentValue); works good.

But, when I run ngFor loop in DOM it does not work.

enter image description here

html

<div class="col-9" *ngFor="let data of currentValue">
  <div formGroupName="questions">
    <div class="form-Group">{{ data.sno }}</div>
      <br>
      <div class="from-Group">{{ data.question}}</div>
    </div>
  <div formGroupName="options">
    <label>
      <input type="radio" formControlName="op1">{{ data.options}}
    </label></div>
5
  • What's the output of console.log ? Commented Apr 22, 2018 at 9:42
  • what does console.log('current Value', this.currentValue); return? Commented Apr 22, 2018 at 9:42
  • current Value [{…}] 0 : formType : "" items : Array(1) 0 : {options: {…}, question: "Test Msg", sno: 1} length : 1 proto : Array(0) proto : Object length : 1 proto : Array(0) console output Commented Apr 22, 2018 at 9:56
  • 2
    The elements in your area to don't seem to have the properties you are looking for in the html Commented Apr 22, 2018 at 10:39
  • now how I fix it? Commented Apr 22, 2018 at 15:43

1 Answer 1

1

The options, question and sno properties are properties of the elements in the items-array of each data-object. Therefore you can nest another *ngFor inside the one you have. Like this:

<div class="col-9" *ngFor="let data of currentValue">
  <div *ngFor="let item of data.items">
    <div formGroupName="questions">
      <div class="form-Group">{{ item.sno }}</div>
        <br>
        <div class="from-Group">{{ item.question }}</div>
      </div>
    <div formGroupName="options">
      <label>
        <input type="radio" formControlName="op1">{{ item.options }}
      </label>
    </div>
  </div>
</div>

Allthough item.options is an object you might want to handle that another way depending on how it is structured.

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

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.