Giter Club home page Giter Club logo

behavior-subject-example's Introduction

Behavior Subject Example

Shared Service

export class SharedService {
  // --- sets up variable as behavior subject of given type (bool in this case) and gives it initial value
  // --- initial value is mandatory, compared to 'Subject' type
  // --- when the value is passed to behavior subject with .next() it broadcasts the new value all subscribers
  // --- BehaviorSubject can be regarded as broadcast variable
  isLogged$: BehaviorSubject<boolean> = new BehaviorSubject(false);
  isAdmin$: BehaviorSubject<boolean> = new BehaviorSubject(false);
}

Login Component HTML

<div>
  <!-- Pushes data to BehaviorSubject via method -->
  <a (click)="loginClient()">Login as Client</a>
  <a (click)="loginAdmin()">Login as Admin</a>
  <a (click)="logout()">Logout</a>
</div>

Login Component

export class LoginComponent {
  // --- registers shared service to gain access to behavior subject variables
  constructor(private shared: SharedService) { }

  // --- sends data to shared service and updates the behavior subject
  loginClient(): void {
    this.shared.isLogged$.next(true);
    this.shared.isAdmin$.next(false);
  }

  // --- sends data to shared service and updates the behavior subject
  loginAdmin(): void {
    this.shared.isLogged$.next(true);
    this.shared.isAdmin$.next(true);
  }

  // --- sends data to shared service and updates the behavior subject
  logout(): void {
    this.shared.isLogged$.next(false);
    this.shared.isAdmin$.next(false);
  }
}

Content HTML

<div>
  <!-- Using async pipe subscribes to changes in behavior subject variables -->
  <p>Logged in: {{ shared.isLogged$ | async }}</p>
  <p *ngIf="(shared.isLogged$ | async)">Logged in as {{ (shared.isAdmin$ | async) ? "Admin" : "Client" }}</p>
</div>

Content Component

export class ContentComponent {
  // --- registers shared service to gain access to behavior subject variables
  constructor(private shared: SharedService) {}
}

behavior-subject-example's People

Contributors

angular-cli avatar milostomsik1 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.