Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does $ mean in Angular 7

I have some confusion in variable declaration.

what does $ mean in heroes$

Angular 4

export class HeroSearchComponent implements OnInit {
heroes: Observable<Hero[]>;
private searchTerms = new Subject<string>();

constructor(
 private heroSearchService: HeroSearchService,
 private router: Router) {}

Angular 7+

 export class HeroSearchComponent implements OnInit {
 heroes$: Observable<Hero[]>;
 private searchTerms = new Subject<string>();
 constructor(private heroService: HeroService) {}

     // Push a search term into the observable stream.
          search(term: string): void {
          console.log(term);
            this.searchTerms.next(term);
        }
  • i have confusion in angular 7 ver where heroes is assigned with $ variable.

  • i tried to remove $ symbol from var then code doesn't works.

  • any suggestion is most welcome

like image 285
afeef Avatar asked Jan 28 '19 07:01

afeef


People also ask

What does !: Means in Angular?

This specifically means that if the value you are binding to the view is null then it should return null else the actual value, so that you dont get any issues while rendering the template.

Why do we use dollar in Angular?

Specifically when it comes to $event and its use in Angular, it is more of a convention carried over from the days of AngularJS. In AngularJS, internal variables exposed by the framework were prefixed with a $ . That is why sometimes you see event and other times $event .

What is .subscribe in Angular?

Normally Subscription means an arrangement to receive something. Similarly, in Angular applications Observables will be connected to observers and whenever they observe a new value or change in data, they will execute code with the help of Subscription and all the subscribed components will receive the updated outcome.

What is multicasting in Angular?

Multicasting is the practice of broadcasting to a list of multiple subscribers in a single execution. With a multicasting observable, you don't register multiple listeners on the document, but instead re-use the first listener and send values out to each subscriber.


1 Answers

It's a convention followed for Observables. Here's what the Angular Docs have to say about it:

Because Angular applications are mostly written in TypeScript, you will typically know when a variable is an observable. Although the Angular framework does not enforce a naming convention for observables, you will often see observables named with a trailing “$” sign.

This can be useful when scanning through code and looking for observable values. Also, if you want a property to store the most recent value from an observable, it can be convenient to simply use the same name with or without the “$”.

There's no harm in not following it as such. But since it is a recommendation, it's good to follow.

UPDATE

Conventions evolve over time depending on the past experience of developers. This particular convention was committed on the 10th of Jan, 2018

So yeah, this convention was added to the Docs after Angular 5 and there are high chances that you weren't using it while working in Angular 4.

Also, the syntax for Rxjs has also changed significantly after the upgrade to Rxjs 5.5 in Angular 5(not sure about the exact version). So you might want to check how the syntax of Rxjs has changed over time. There's a nifty tool to help you with that. Check out RxJS Explorer 2.0: Learn. Compare. Update.

like image 148
SiddAjmera Avatar answered Sep 28 '22 17:09

SiddAjmera