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 used | Example | Explanation |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
๐ธ 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
๐ HTTP (@angular/common/http
)
-
HttpHeaders
-
HttpParams
๐ RxJS
-
Subject
-
BehaviorSubject
-
ReplaySubject
๐ Date & Utilities
-
Date
-
Custom class instances
❗ Error Classes
-
HttpErrorResponse
(sometimes) -
Custom error classes
-n
shows line numbers-
-w
matches the whole word
Internal Usages (some examples)
Module/Area | Used with new | Purpose |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|