Common places and scenarios where new keyword is used in Angular projects

 In Angular, the new keyword is used to create instances of classes. While Angular primarily relies on Dependency Injection (DI) for services and components, you may explicitly use new in specific scenarios. Below is a categorized list of common use cases:

Common Use of new in Angular

Where it's usedExampleExplanation
  • Creating a class instance
  • let person = new Person();
  • Standard JavaScript usage in services or components
  • Creating Dates
  • let now = new Date();
  • Frequently used in date pickers or logging
  • Creating FormGroups
  • new FormGroup({...})
  • Reactive Forms: defining a new form group
  • Creating FormControls
  • new FormControl('value')
  • Reactive Forms: defining individual form controls
  • Custom Error Classes
  • throw new CustomError('Something went wrong');
  • Angular apps often define custom error classes
  • HttpHeaders or HttpParams
  • new HttpHeaders().set('Authorization', 'Bearer')
  • Used to configure HTTP requests
  • Creating Observables
  • new Observable(observer => {...})
  • Sometimes used to create custom observables
  • Using new in RxJS operators
  • new Subject() or new BehaviorSubject()
  • Common in services or state management
  • Dependency Injection (rare)
  • new SomeService() (not recommended)
  • Not recommended; use Angular DI instead

🔸 Angular-Specific Classes Often Instantiated with new

These are common classes you might instantiate with new:

  • FormControl

  • FormGroup

  • FormArray

  • HttpHeaders

  • HttpParams

  • DatePipe, CurrencyPipe, etc. (though usually injected, sometimes instantiated manually)

  • BehaviorSubject, Subject, ReplaySubject (from RxJS)

  • Custom model classes (e.g., new UserModel())

  • Custom error classes


✅  Common Angular Classes You Instantiate with new

These are safe and common classes that you can instantiate with new in your Angular application:

🔹 Frequently Used with new

📦 Forms (@angular/forms)

  • FormControl

  • FormGroup

  • FormArray


const name = new FormControl('John'); const form = new FormGroup({ name: name });

🌐 HTTP (@angular/common/http)

  • HttpHeaders

  • HttpParams


const headers = new HttpHeaders().set('Authorization', 'Bearer token');

📊 RxJS

  • Subject

  • BehaviorSubject

  • ReplaySubject


const subject = new BehaviorSubject<number>(0);

📆 Date & Utilities

  • Date

  • Custom class instances


const date = new Date(); const user = new UserModel();

❗ Error Classes

  • HttpErrorResponse (sometimes)

  • Custom error classes

  • -n shows line numbers

  • -w matches the whole word

 Internal Usages (some examples)

Module/AreaUsed with newPurpose
  • @angular/forms
  • new FormControl(), new FormGroup()
  • Used in reactive forms
  • @angular/common/http
  • new HttpHeaders(), new HttpParams()
  • Used to configure HTTP requests
  • @angular/core/testing
  • new TestBed() (indirectly)
  • Internally during unit testing

  • RxJS (used in Angular)
  • new Subject(), new BehaviorSubject()
  • Angular uses these for observable patterns

No comments:

Post a Comment

starter ASP.NET Core Web API project with example

Starter ASP.NET Core Web API project that follows all the best practices listed above. 🛠️ Starter Project Overview We’ll build a Produc...

Best for you