0

as I saw in many tutorials, it is highly recommended to use native way of Angular if we are dealing with an observable service. So I have a service like "ActionService" with a method:

findAll(): Observable {
  return this.http.post(this.baseUrl)
}

My Component: export class ActionCodesComponent { actionCodes$: Observable

constructor(private actionService: ActionService) {
this.actionCodes$ = this.actionService.findAll();
}

onDelete() { // how should I refresh the Observable here???
this.actionCodes$ = this.actionService.findAll();
}

trackByActionCode(index, actionCode): string {
return actionCode ? actionCode.id : undefined
}

} The first problem: right now, if I delete an element from a list by clicking on "X" span, the whole list is being rendered again and flicking. So at first the trackBy function does not work (if I use the same function not with observable but with array of elements, it works very fine).

Second Problem: how can I update my Observable without recreation it again like

this.actionCodes$ = this.actionService.getAll()

? All examples on youtube and internet showing like myObservable = the Subject() and myObservable.next(). But it does bot work with my Observable this.actionCodes$. There is no next() method available at all.

Can somebody tell me what I am doing wrong here? If I cannot update a list using Observable (not an array), why it is recommended by every Angular expert? Thanks

RomanN
  • 1
  • 2
  • 1
    If you are using an `async` pipe with your `actionCodes$` maybe you will find that question useful: https://stackoverflow.com/questions/44805203/trackby-with-the-async-pipe – George Pushia Apr 12 '22 at 13:37
  • About second question. `Subject` and `Observable` are often mistaken. With `Observable` you can only consume/get data. `Subject` on the other hand can both produce data (so `emit()`) and consume `subscribe()`. [Here is thread about it](https://stackoverflow.com/questions/47537934/what-is-the-difference-between-observable-and-a-subject-in-rxjs) – Yodde Apr 12 '22 at 14:30
  • That article you pointed I have already read. It is exactly what I can not repeat, it is recommended, that I use ..$.next(), which is not working in my case. Since I have no subject. – RomanN Apr 13 '22 at 11:23

0 Answers0