0

I am trying to implement a multilevel questions-answers tree. More specifically, there will be one root question and then one would have the opportunity to create multiple answers for that same root question. Depending on the answer, there will be another question-answer pair. Again, this new question now has the possibility of several answers just like the root one. And this will continue at least for five steps. Same will applicable for the other answers of the root question and so on and so forth... For the clarification, I have attached a screen-shot.

enter image description here

I have tried for couple of days to achieve this functionality. However, I couldn't get the expected solution. I have tried with Angular reactive forms. For this, I followed these approaches (1) https://stackblitz.com/edit/angular-4dt3sa?file=src%2Fapp%2Fapp.component.ts

Now I am trying this code. This was implemented in AngularJS. However, converting this into Angular 6 does not help me to get out of this riddle.

Code after converting to Angular 6.

component.ts:

import { Component, ViewChild, OnInit } from '@angular/core';

@Component({
  selector: 'app-analysis-form',
  templateUrl: './analysis-form.component.html',
  styleUrls: ['./analysis-form.component.css']
})
export class AnalysisFormComponent implements OnInit {

  tree = [{ name: 'Node', nodes: [] }];

  constructor() { }

  ngOnInit() { }

  delete(data) {
    data.nodes = [];
  }
  add(data) {
    const post = data.nodes.length + 1;
    const newName = data.name + '-' + post;
    data.nodes.push({ name: newName, nodes: [] });
  }

}

html:

{{data.name}}
<button (click)="add(data)">Add node</button>
<button (click)="delete(data)" [hidden]="!data.nodes.length > 0">Delete nodes</button>
<ul>
  <li *ngFor="data in data.nodes">
    <app-analysis-form></app-analysis-form>
  </li>
</ul>

<ul>
  <li *ngFor="data in tree">
    <app-analysis-form></app-analysis-form>
  </li>
</ul>

Thanks in advance for suggestions.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Yasir Arefin
  • 351
  • 1
  • 9
  • 23
  • What's not working in the last example? – Christian Vincenzo Traina Dec 15 '18 at 10:59
  • It shows this error "Uncaught Error: Template parse errors: Can't bind to 'ngFor' since it isn't a known property of 'li'. ("
    • ]*ngFor="data in data.nodes">
    • ")". And I am not sure if this is the right way to implement the recursion. Thanks
    – Yasir Arefin Dec 15 '18 at 11:12
  • 1
    Possible duplicate of [Exception: Can't bind to 'ngFor' since it isn't a known native property](https://stackoverflow.com/questions/34012291/exception-cant-bind-to-ngfor-since-it-isnt-a-known-native-property) – JJWesterkamp Dec 15 '18 at 11:57
  • The syntax is `*ngFor="let element of array"`, not `*ngFor="element in array"`. – JB Nizet Dec 15 '18 at 12:44

0 Answers0