• Skip to main content
  • Skip to primary sidebar

Web Development Archive

  • Archive
You are here: Home / Other / Angular Boolean Service

Angular Boolean Service

Service

BehaviorSubject létrehozása, amely egy figyelhető boolean értéket tárol.

private readonly _isLoginPageBs$ = new BehaviorSubject<boolean>(false);

Observable létrehozása, hogy a külső kód figyelhesse, de ne módosíthassa az állapotot.

public readonly isLoginPageObs$ = this._isLoginPageBs$.asObservable();

Getter metódust definiál, amely az _isLoginPageBs$ aktuális értékét (value) adja vissza szinkron módon.

public get isLoginPage(): boolean {
return this._isLoginPageBs$.value;
}

Setter metódust definiál, amely az _isLoginPageBs$ értékét frissíti a megadott isLoginPage értékre.

public set isLoginPage(isLoginPage: boolean) {
this._isLoginPageBs$.next(isLoginPage);
}

Parent component

private readonly _headerNavService = inject(HeaderNavService);
public isLoginPage: boolean;

public ngOnInit(): void {
this._initIsLoginPage();
}

private _initIsLoginPage(): void {
this._headerNavService.isLoginPageObs$.pipe(takeUntil(this._destroy$)).subscribe((isLoginPage: boolean) => {
this.isLoginPage = isLoginPage;
this._cdr.markForCheck();
console.log(isLoginPage);
});
}

Child component

private readonly _headerNavService = inject(HeaderNavService);
@Input() public isLoginPage: boolean;

public ngOnInit(): void {
this._setLoginPage();
}

private _setLoginPage(): void {
this._headerNavService.isLoginPage = true;
}

Filed Under: Other

About Gabor Flamich

I'm a web developer and designer based in Budapest, Hungary. In recent years, I've documented hundreds of solutions I came across during development. This site is an archive for useful code snippets on WordPress, Genesis Framework and WooCommerce. If You have any questions related to WordPress development, get in touch!

Primary Sidebar

  • angular.io
© 2026 WP Flames - All Right Reserved