jeudi 9 juin 2016

Angular 2 persist state of component for re-creation

Consider the following scenario: Category component has a list of questions, which may change by user event. It's most suitable to render each question as an individual view by iteration, as number of the questions is unknown. Thus Question component.

selector: 'category'
template: <question *ngFor="question of questions" [question]=question />
export class Category {
  questions: Question[] = questionsA;
  ... internal state ...
}

selector: 'question'
export class Question {
  @Input() question: Question;
  ... internal state ...
}

Now, if the questions array questionsA changes to questionsB, brand new Question components are instantiated which is fine. However, this is also the case when questions array changes back to original questionsA. But I want to retrieve those original components with their state, and not new components.

Does Angular 2 provide a mechanism to reuse the component, which was earlier instantiated with the same object by *ngFor, or is my only choice to persist and poll the state of component by question with an injected service?

Aucun commentaire:

Enregistrer un commentaire