I was wondering what was the correct way to access data in a component. I've read a couple times already that you should let angular templates handle subscriptions But how would you access current values for certain methods inside the component.ts file?
Suppose i have the following component:
@Component({...})
class TestComponent {
testData: Observable<TestData>
ngOnInit(){
this.testData = this.service.getData().pipe(share())
}
}
It has a sendDataABCProperty which sends the property ABC of our testData to some arbitrary service.
sendDataABCProperty() {
this.testData.pipe(first()).subscribe(data => this.service.sendData(data.abc))
}
Now, when using the above approach i still have to subscribe to my data, exactly what i was trying to prevent.
Another way would be to copy the data and not use a stream, but since i'm trying to make it more reactive, that's a way i'ld like to avoid.
A further example would be some kind of "display and edit" component.
Suppose I get the my display data from some service who I access afterwards aswell to update the data. But i need the id (of the data) for it. What would be the best way to access the id? Copying it at the beginning (using tap() / subscribe())? Using a BehaviorSubject? Or something completely different?
subscribe().