Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

button show or hide content – angular BehaviourSubject

I have a button, that on every click should display or hide content

html:

            <button (click)="showHide()">
                    {{ content$ ? 'Hide'  : 'show'  }}
            </button>
            <div *ngIf="content$" >
             CONTENT
            </div>

ts:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

readonly content$ = new BehaviorSubject<boolean>(false);
showHide(): void {
    this.content$.next(true);
}

and this code does not gives any error, but it always displays content and i cannot hide it, any help?

>Solution :

Hello and welcome to Stackoverflow!

You are correctly set a new value into the BehaviorSubject, and with: *ngIf="content$"you are just checking that BehaviorSubject exists.

In order to get the content you have to:

<div *ngIf="content$ | async; let content">
  CONTENT
</div>

With let content you can access to your subject’s value, in case you have to 😉

You can have more information about on AsyncPipe doc

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading