0

I have a scenario where I'm trying to show radio buttons say one question has two answers as 1) Veg 2)Non-veg ,so when i select veg it has further three options and the third option "Mixture of Vegetable and Fruits" has further question inside it and so on.

The following is the reference json that I'm using

"choices": {
      "1": {
        "text": "Vegeterian",
        "questions": {
          "1": { 
            "choices": {
              "1": { "text": "Only Veggies" },
              "2": { "text": "Only Fruits" },
              "3": {
                "text": "Mixture of Vegetable and Fruits",
                "questions": {
                  "1": {
                    "type_name": "selectOne",
                    "choices": {
                      "1": {
                        "text": "Yes.. No animal product",
                        "questions": {
                          "1": {
                            "type_name": "selectOneOrMore",
                            "choices": {
                              "1": {
                                "text": "You are a nice human being"
                              },
                              "2": {
                                "text": "You are a good human being"
                              }
                            }
                          }
                        }
                      },
                      "2": {
                        "text": "No meat. but other animal product"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "2": { "text": "Non-Vegeterian"}
    }

and on the angular front I have just shown the outer two option ,how can i dynamically on selection of the radio button add further question if it has further questions and option

    <ng-container *ngFor="let consult of choices | keyvalue; let j = index; trackBy: trackByFn;">
              <td>
                   <input  type="radio" name="radio" id="radio-{{j}}" />
                   <label class="sr-text" for="radio-{{j}}">{{consult.value.text}}</label>
              </td>
   </ng-container>
2
  • You can use ng-if to render item based on conditions, knowing that ng-if will not add the item to DOM till the condition met, and when condition fail it'll remove it from DOM, as well as you might give this link stackoverflow.com/questions/44411991/… a look Commented Sep 1, 2020 at 9:59
  • @AbdulrahmanFalyoun that would be great approach if i have a fixed number or question depth but here we cant be sure about the question depth Commented Sep 1, 2020 at 10:03

1 Answer 1

0

You can create a component that renders a "question" and a component that renders a "choice". If the choice is itself a question, you use the components recursively.

Some code to show this approach (not tested and you have to adapt it to your own data structure):

@Component({
  template: `
  ...
  <div ngFor="choice in question.choices">
    <appChoice [choice]="choice"
  </div>
  `
})
class QuestionComponent {
  @Input() question;
  ...
}

@Component({
  template: `
  <div ngIf="isQuestion; else notQuestion">
    <appQuestion [choices]="choice.question">
  </div>
  <ng-template #notQuestion>
    ...
  </ng-template>
  `
})
class ChoiceComponent {
  @Input() choice;
  get isQuestion() { return !!this.choice.question; }
  ...
}
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.