BehaviorSubject на refre sh на радио-вопрос, чтобы сохранить ценность - PullRequest
0 голосов
/ 06 мая 2020

Я использую 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'] || [];
  }
}

1 Ответ

0 голосов
/ 06 мая 2020

Попробуйте вместо

value: this.inUK$.value

то, что возвращает значение, когда возвращается Radio Question

value$: this.inUK$

и в HTML конвейере с asyn c

value$ | async
...