Search code examples
angulardecoratorangular-decorator

Why does Angular 2 use decorators?


I just started using Angular 2 and was wondering why some properties like selector and template are put in components decorators and not in components classes.

What's the point of using all these decorators in Angular 2?


Solution

    • To make it easy for tools to provide all kinds of support in templates like:

      • error checking
      • auto-completion
      • graphical GUI designers
    • To generate code from decorators which allows:

      • to define some things more declaratively or
      • generate different code depending on some configuration (like the upcoming offline template compiler does)

    Code would need to be executed to use results expressions might emit. Decorators can be easily evaluated statically without executing the TypeScript code (except maybe a simple and limited subset).