My application is using Angular and ngrx store. I am using a selector in the store to get the username property of the app state in my component constructor:
test-component.ts
export class TestComponent {
username$: Observable<string>;
constructor(private store: Store<fromRoot.AppState>) {
this.username$ = this.store.select(fromRegistration.getUsername);
}
testMethod() {
//I need the value of the username observable here as a string
}
}
This is working fine. I need to print the username in the component's template, which I am doing by using the async pipe:
test-component.html
<div>
{{username | async}}
</div>
This is also working correctly.
Now, I need to call a service from a method in TestComponent that sends the username as an argument. How can I achieve this? Do I need to stop using the async pipe and subscribe to the selector to then assign the username property (which would then be declared as a string) to the value of the observable?
To note: the username observable will only have 1 value, once I get 1 value I can stop watching it.
I understand by using the async pipe the component unsubscribe from the username observable automatically, when does this happen and how does the component knows I don't need to watch it anymore?
If I can't use the async pipe, how and when do I unsubscribe?