Dear experts:
I want to save an input value to a behavior subject. I want the input value to stay as a visible value once the user refreshes the page. I know how to do it with session storage and I now I want to try it with RxJs. I don't unserstand why my code isn't working. Thanks in advance
appcomponent:
import { Component, OnInit, VERSION } from '@angular/core';
import { InputValueService } from './Services/input-value.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
public inputValue = '';
constructor(private observeInput: InputValueService) {}
ngOnInit(): void {
this.inputValue = this.observeInput.inputValue.getValue();
}
setSessionStorage(value: string) {
this.inputValue = value;
console.log(this.inputValue);
return this.inputValue;
}
}
appcomponent.hml
<input #box (keyup)="setSessionStorage(box.value)" [(ngModel)]="inputValue" />
<p>{{ inputValue }}</p>
inputservice.
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable()
export class InputValueService {
public inputValue = new BehaviorSubject<string>('');
constructor() {}
}
stackblitz: https://angular-ivy-kxmwby.stackblitz.io