Angular Subject Example



1. user.service.ts
private jumpTimeSlotSource = new Subject<string>();
public jumpTimeSlot$ = this.jumpTimeSlotSource.asObservable();
public onJumpTimeSlot(timeId: any) {
const self = this;
self.jumpTimeSlotSource.next(timeId);
}
2. user.component.ts
public onClickRoute(timeId: any) {
const self = this;
self.userService.onJumpTimeSlot(timeId)
}
3. user.component.html

<button (click) ="onClickRoute('100001')">Route</button>;

4. user-edit.component.ts
public subscription: Subscription;
constructor(private userService: UserService, private _router: Router) {
const self = this;
self.subscription = self.userService.jumpTimeSlot$.subscribe((res) => {
self._goToAnchor1(res);
});
}
private _goToAnchor1(timeId: any) {
const self = this;
self._router.navigate(['/user', 'activity'], { fragment: timeId });
}
  
ngOnDestroy() {
this.observer.disconnect();
this.subscription.unsubscribe();
}
Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment