I have the following objects:
class Question {
idQuestion: string;
question: string;
typeQuestion: string;
}
class Answer {
idAnswer: string;
idQuestion: string;
answer: string;
}
class Option {
idOption: string;
idQuestion: string;
option;
}
And I want to populate the following object:
class QuestionOptionsAnswer {
question: Question;
answer: Answer;
options: Option[];
}
For now, I have a service for each kind of object, so we can illustrate it in the following way:
questionService.getQuestions();
answerService.getAnswers();
optionService.getOptions();
To populate a questionoptionsanswer
object I have doing nested requests:
questionService.getQuestions()
.subscribe(
answerService.getAnswers()
.subscribe(
optionService.getOptions()
.subscribe();
)
)
I can populate correctly the questionoptionsanswer
object, but is is slow, so I think I am making a bad approach. The idea behind having a questionoptionsanswer
is for rendering in a easy way the html
with angular2 directives.
I read about flatMap
, switchMap
, forkJoin
but I am not quite sure in how to use them.
Which could be a good approach to load this data?
questionoptionsanswer
will have a question object, an answer associated with it, and its possible options depending on the typeQuestion
i.e: select, radio, multiple, etc.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…