Я использую Angular 7. Я делаю форму, в которой используется радио-вопрос, чтобы спросить, произошло ли это в конкретной стране. Однако, когда пользователь переходит вперед, а затем возвращается, я хотел бы, чтобы значение было сохранено
questions.service.ts
В этом файле у меня есть следующее:
export class QuestionsService {
inUK$ = new BehaviorSubject(null);
getQuestions(): QuestionBase<any>[] {
const questions = [
new RadioQuestion({
key: COUNTRY,
label: 'Did it happen in the UK',
options: [
{key: true, value: 'Yes'},
{key: false, value: 'No'},
],
type: 'other',
value: this.inUK$.value
}),
}
setIsInUK$() {
this.inUK$.next(true);
}
setIsNotInUK$() {
this.inUK$.next(false);
}
}
Как сохранить значения BehaviorSubject . Я пробовал asObservables.
Другие файлы:
question-base.model.ts
import {Observable, of} from 'rxjs';
export class QuestionBase<T> {
value: T;
key: string;
label: string;
controlType: string;
type: string;
constructor(options: {
value?: T,
key?: string,
label?: string,
type?: string,
} = {}) {
this.value = options.value;
this.key = options.key;
this.label = options.label;
this.type = options.type || 'other';
}
}
radio-question.model.ts
export class RadioQuestion extends QuestionBase<string> {
controlType = 'radio';
options: {key: string, value: string}[] = [];
constructor(options: {} = {}) {
super(options);
this.options = options['options'] || [];
}
}