Indeed, Angular handles most of the imperative tasks related to the lifecycle of instances. Here are some key things that Angular manages for you:
- Every service, factory, and provider declared in Angular provides Angular with either a constructor function or an instance created by a factory. Constructors return objects with a
$get()
method that yields an instance of the provider. You can learn more about this concept here.
- All services in Angular are singletons, meaning they are only instantiated once (typically when first injected into a controller, directive, or other component). This allows for shared state among different entities that use the same service.
- Under the hood, Angular creates providers for each service/factory with the provider's name followed by "Provider."
Controllers, directives, and filters behave similarly to services but require special handling due to scope/input injection requirements. Angular takes care of managing these components as well.
After using Angular extensively for six months, I've realized that I rarely need to use the new
keyword anymore. Angular efficiently handles instance creation and destruction so you don't have to worry about it.