I'm working with a basic CRUD which should be saved in localStorage
, and obviously with all operations working normally (register, delete and update). No problems with this part, I'm working with temporary arrays and only later saving in localStorage
.
My problem is with the listing, I need to do it asynchronously, that is, when localStorage
goes through modifications, the list must be updated dynamically. I know the procedure would be easy using API, with only subscribe
, but the challenge is precisely this. I've been researching topics here in the OS and several documentation of the Angular, I found that the best way would be to use BehaviorSubject
, but I'm not able to implement.
service.ts
@Injectable()
export class ItemService {
// valor default = localStorage
public itens: BehaviorSubject<any> = new BehaviorSubject(JSON.parse(localStorage.getItem('itens')));
[...]
setItem(item: Item) {
localStorage.setItem('itens', JSON.stringify(item));
this.itens.next(item); // problema no next()...
}
Component.ts
ngOnInit() {
this.itemService.itens.subscribe(itens => {
this.dados = itens;
});
[...]
I have already tried to implement using EventEmitter and Observable
I'm not stuck with BehaviorSubject
, any suggestions or answers that help solve the problem will be very useful.