Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 style guide - property with dollar sign?

Parent and children communicate via a service example from the official guide on Angular.io makes use of dollar signs in Observable stream names.

Notice missionAnnounced$ and missionConfirmed$ in the following example:

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable()
export class MissionService {

  // Observable string sources
  private missionAnnouncedSource = new Subject<string>();
  private missionConfirmedSource = new Subject<string>();

  // Observable string streams
  missionAnnounced$ = this.missionAnnouncedSource.asObservable();
  missionConfirmed$ = this.missionConfirmedSource.asObservable();

  // Service message commands
  announceMission(mission: string) {
    this.missionAnnouncedSource.next(mission);
  }

  confirmMission(astronaut: string) {
    this.missionConfirmedSource.next(astronaut);
  }
}

Can anyone explain:

  • why $ is used? What's the reason behind this notation? Do I always need to use this for public properties?
  • public properties are used but not methods (e.g. missionAnnouncements(), missionConfirmations()) - again, is this a convention for Angular2 apps?
like image 739
gerasalus Avatar asked Oct 05 '22 07:10

gerasalus


People also ask

What does a dollar sign mean in angular?

$ suffix (popularized by Cycle.js) is used to indicate that the variable is an Observable. It could make it to the official style guide too but it's not there yet.

Why do we use dollar in angular?

An Observable is a stream of values emitted over time. In code, it is often represented with the dollar sign $ as a suffix. For example, by using the fromEvent operator, we can create an observable that listen to changes to a DOM element element on input events.

What does dollar mean in TypeScript?

A $ dollar sign in JavaScript (and TypeScript by extension) is a valid identifier to use as part of a variable name, it carries no syntactical meaning. It can be used in the beginning, middle or end of a variable/function name.

What is Finnish notation?

In Finnish-Goldman notation, what you do is pluralize observable variable names with a unicode character that matches the last letter of the pluralized word. For example: const oxeÑ = Observable. from(olleyOlleyOxenStream()); const mic€ = Observable.


1 Answers

$ suffix (popularized by Cycle.js) is used to indicate that the variable is an Observable. It could make it to the official style guide too but it's not there yet

Read more here : What does the suffixed dollar sign $ mean?

Update: Read more about the trailing “$” sign on Angular website here: https://angular.io/guide/rx-library#naming-conventions-for-observables

like image 181
Monfa.red Avatar answered Oct 17 '22 11:10

Monfa.red