Imagine a list of users displayed in an Angular component. I inject my service and subscribe to it in my template.
I also have a dialog where you can add users to my table. After adding and closing my dialog, I want to refresh the table via re-triggering the service call.
component:
export class UserComponent implements OnInit {
public users$!: Observable<User[]>;
constructor(
public dialog: MatDialog,
private userService: UserService,
) {}
public ngOnInit() {
this.users$ = this.userService
.getAllUsers$()
.pipe(take(1));
}
public openAddUserDialog() {
this.dialog
.open(AddUserDialogComponent)
.afterClosed()
.subscribe(() => {
// TODO re-trigger observable here?
});
}
}
template:
<p *ngFor="user of users$ | async">{{ user.name }}</p>
How do I re-trigger the service call in the afterClosed()
function from the dialog?
I do not want to subscribe this in my subscribe.
I believe there must be a pattern to join the return value of the getAllUsers$()
with another Observable and push something there and then re-trigger the process.