What is Decorators in Angular 5
- Decorators are a features of type Script and are implemented as functions. The name of the decorators starts with @ symbols following by brackets and arguments, Since decorators are just function in type Script.
- Decorators are simply functions that return functions. These functions supply detadata to angular about a particular class, property, value or method.
- Decorators are invoked at run-time.
- Decorators allow you to execute functions. For example @Component executes the Component function imported from Angular 5.
Some Common Decorators
- @NgModule() to define modules
- @Compomnent() to define components
- @Injectable() to define services
- @Input() and @Output() to define properties that send and receive data from DOM
- There are many built-in decorators available in angular and many properties on each decorators
Types of Decorators
- Class Decorators – e.g. @Component and @NgModule
- Property Decorators for properties inside class- e.g. @Input and @Output
- Method Decorators for methods inside classes- e.g @HostListner
- Parameter Decorators for parameters inside class constructors – e.g. @Inject
- Each decorators has unique role.