Skip to content

Instantly share code, notes, and snippets.

@zzpzaf
Created July 14, 2024 08:45
Show Gist options
  • Save zzpzaf/86b0184013de59b230ae9ecde3900585 to your computer and use it in GitHub Desktop.
Save zzpzaf/86b0184013de59b230ae9ecde3900585 to your computer and use it in GitHub Desktop.
signals-data-sharing-service-1
import { Injectable, signal } from '@angular/core';
export interface IMsg {
sender: string;
msg: string;
}
@Injectable({
providedIn: 'root'
})
export class ShareDataService {
public myMsg: IMsg = {sender: '', msg: ''};
public $Msg = signal< IMsg>(this.myMsg);
constructor() {
// this.messageSubject.next({sender: this.constructor.name, msg: 'This is the Initial Behavior Subject message.'});
}
// public messageSubject = new BehaviorSubject<IMsg>({sender: this.constructor.name, msg: 'This is the Initial Behavior Subject message.'});
public setData(message: IMsg) {
console.log( '>===>> ShareDataService - ' + 'setData - '+ 'Setting message value in sharedService: ' + message.sender + ' - ' + message.msg );
// this.messageSubject.next(message);
//this.$Msg.update(() => message);
this.$Msg.set(message);
}
// public getData(): Observable<IMsg> {
public getData(): IMsg{
console.log( '>===>> ShareDataService - ' + 'getData - '+ 'Getting message value from sharedService.');
// return this.messageSubject.asObservable();
return this.$Msg();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment